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
iOS difference between isKindOfClass and isMemberOfClass
提问by NoodleOfDeath
What is the difference between the isKindOfClass:(Class)aClass
and the isMemberOfClass:(Class)aClass
functions?
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)aClass
和isMemberOfClass:(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 YES
if the receiver is an instanceof the specified class oran instance of any class that inheritsfrom the specified class.
isKindOfClass:
YES
如果接收者是指定类的实例或从指定类继承的任何类的实例,则返回。
isMemberOfClass:
returns YES
if, 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 classisMemberOfClass:
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 类和协议见苹果文档: