isKindOfClass 和 isMemberOfClass 之间的 iOS 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3653929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:40:45  来源:igfitidea点击:

iOS difference between isKindOfClass and isMemberOfClass

iosobjective-cswiftclassinheritance

提问by NoodleOfDeath

What is the difference between the isKindOfClass:(Class)aClassand the isMemberOfClass:(Class)aClassfunctions? I know it is something small like, one is global while the other is an exact class match but I need someone to specify which is which please.

功能isKindOfClass:(Class)aClassisMemberOfClass:(Class)aClass功能有什么区别?我知道它很小,一个是全局的,而另一个是精确的类匹配,但我需要有人指定哪个是哪个。

In Swift isKind(of aClass: AnyClass)and isMember(of aClass: AnyClass).

在 SwiftisKind(of aClass: AnyClass)isMember(of aClass: AnyClass).

回答by Sebastian Celis

isKindOfClass:returns YESif the receiver is an instanceof the specified class oran instance of any class that inheritsfrom the specified class.

isKindOfClass:YES如果接收者指定类的实例从指定类继承的任何类的实例,则返回。

isMemberOfClass:returns YESif, and only if, the receiver is an instanceof the specified class.

isMemberOfClass:YES当且仅当接收者指定类的实例时返回。

Most of the time you want to use isKindOfClass:to ensure that your code also works with subclasses.

大多数情况下,您希望使用它isKindOfClass:来确保您的代码也适用于子类。

The NSObject Protocol Referencetalks a little more about these methods.

NSObject的协议参考说话,稍微介绍一下这些方法。

回答by jtbandes

  • isKindOfClass:indicates whether an object inheritsfrom a given class
  • isMemberOfClass:indicates whether an object is an instance ofa given class.
  • isKindOfClass:指示对象是否继承自给定类
  • isMemberOfClass:指示对象是否给定类的实例


[[NSMutableData data] isKindOfClass:[NSData class]]; // YES
[[NSMutableData data] isMemberOfClass:[NSData class]]; // NO

回答by kennytm

Suppose

认为

@interface A : NSObject 
@end

@interface B : A
@end

...

id b = [[B alloc] init];

then

然后

[b isKindOfClass:[A class]] == YES;
[b isMemberOfClass:[A class]] == NO;

Basically, -isMemberOfClass:is true if the instance is exactly of the specified class, while -isKindOfClass:is true if the instance is exactly of the specified class or if one of the instance's ancestors is of the specified class.

基本上,-isMemberOfClass:如果实例完全属于指定类,则为-isKindOfClass:真,而如果实例完全属于指定类,或者实例的祖先之一属于指定类,则为真。

-isMemberOfClass:is seldom used.

-isMemberOfClass:很少使用。

回答by Alex Terente

isKindOfClass: Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

isMemberOfClass: Returns a Boolean value that indicates whether the receiver is an instance of a given class.

isKindOfClass:返回一个布尔值,指示接收者是给定类的实例还是从该类继承的任何类的实例。

isMemberOfClass:返回一个布尔值,指示接收者是否是给定类的实例。

回答by Ishu

isKindOfClass-> return YES when the object is instance of that class or instance of a class which is inherited from it.

isKindOfClass-> 当对象是该类的实例或从它继承的类的实例时,返回 YES。

isMemberOfClass: return YES when the object is instance of that class but No in case: instance of a class which is inherited from it.

isMemberOfClass:当对象是该类的实例时返回 YES,但在情况下返回 No:从它继承的类的实例。

example is good enough in jtbandes answer.

jtbandes答案中的例子已经足够了。

回答by Art Swri

Because of class clusters, isMemberOfClass can give you an answer you might not expect. In many cases your best choice is more likely to be -(BOOL)conformsToProtocol:(SEL)aSelector or - (BOOL)conformsToProtocol:(Protocol*)aProtocol. I.e, it's better to test these if they can answer your need rather than testing class/subclass.

由于类集群,isMemberOfClass 可以给你一个你可能意想不到的答案。在许多情况下,您的最佳选择更有可能是 -(BOOL)conformsToProtocol:(SEL)aSelector 或 - (BOOL)conformsToProtocol:(Protocol*)aProtocol。即,如果它们可以满足您的需要,最好测试它们而不是测试类/子类。

See apple doc for NSObject class and protocol:

NSObject 类和协议见苹果文档:

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/cl/NSObject

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/cl/NSObject

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intf/NSObject

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intf/NSObject