isKindOfClass:
returns YES
if the receiver is an instance of the specified class or an instance of any class that inherits from the specified class.
isMemberOfClass:
returns YES
if, and only if, the receiver is an instance of the specified class.
Most of the time you want to use isKindOfClass:
to ensure that your code also works with subclasses.
The NSObject Protocol Reference talks a little more about these methods.
isKindOfClass:
indicates whether an object inherits from a given classisMemberOfClass:
indicates whether an object is an instance of a given class.[[NSMutableData data] isKindOfClass:[NSData class]]; // YES
[[NSMutableData data] isMemberOfClass:[NSData class]]; // NO
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:
is seldom used.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With