Reputation: 4236
I understand the difference between isKindOfClass: and isMemberOfClass: but I came across something I do not understand:
-(UIImage *)doSomething:(id)item
{
UIImage *image;
if ([item isKindOfClass:[NSDictionary class]]) {
NSDictionary *dictionary = item;
NSData *data = [dictionary objectForKey:@"image"];
image = [[UIImage alloc] initWithData:data];
} else { // if item is UIImage
image = item;
}
return image;
}
If I am using isKindOfClass in this context everything works as expected. If I use isMemberOfClass I get the following crash asking for the size of the image later:
-[__NSDictionaryI size]: unrecognized selector sent to instance 0x123456
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI size]: unrecognized selector sent to instance 0x123456'
I read other posts like this one but I couldn't find anything that would come closer.
Upvotes: 2
Views: 1747
Reputation: 56625
Yes they are different and their difference is documented. Using isKindOfClass:
will return YES for subclasses whereas isMemberOfClass:
won't. Since NSDictionary is a class cluster (uses private subclasses internally) will get different results when using the two (because the instance would be a private subclass (in your case __NSDictionaryI
).
When using isMemberOfClass:
, this is what happens in your case:
item
is a private dictionary subclassisMemberOfClass:
returns NOsize
and throws an exception.Upvotes: 4