isMemberOfClass:
will only return YES
if the instance's class is exactly the same, however isKindOfClass:
will return YES
if the instance's class is the same, or a subclass of the given class.
For example this will output No!
:
BOOL result = [[NSMutableArray array] isMemberOfClass:[NSArray class]];
NSLog (@"%@", result? @"Yes!" : @"No!");
But this will output Yes!
:
BOOL result = [[NSMutableArray array] isKindOfClass:[NSArray class]];
NSLog (@"%@", result? @"Yes!" : @"No!");
This is because an NSMutableArray is a kind of NSArray, but it isn't a member of the NSArray class (otherwise it wouldn't be an NSMutableArray).
Throughout Foundation and Cocoa, there are a number of "class clusters". You can read more about this in the documentation on Apple's developer web site. Due to the nature of class clusters, if you create perhaps an NSString
object, it may fail the isMemberOfClass:[NSString class]
test.
If neither isKindOfClass:
or isMemberOfClass:
is returning the correct value, see what class the actual object is with
NSLog(@"cat class = %@, dog class = %@", [cat className], [dog className]);
If these are returning anything other than what they are supposed to, then there is a problem with your farm class.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…