views:

91

answers:

2

Can any one tell me what the difference is 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.

+3  A: 

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 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.

Sebastian Celis
+1  A: 

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 when one of the instance's ancestor is the specified class.

-isMemberOfClass: is seldom used.

KennyTM