Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does NSDictionary report an unusual class name?

NSDictionary *dict = [NSDictionary dictionary];
NSLog(@"%@", NSStringFromClass([dict class])); 

This code prints "__NSDictionary0".

For my own classes it prints the actual class name.

Why is NSDictionary identified as __NSDictionary0, and is it safe to depend on this?

like image 930
stevex Avatar asked Dec 08 '10 18:12

stevex


1 Answers

NSDictionary is a class cluster, as Gendolkari said and Class Clusters are documented.

And, no, you can't depend on the exact identity of the private subclass.

You should certainly be able to do the following to determine if it is a dictionary or not:

[myThingaMaHoover isKindOfClass: [NSDictionary class]];

Or, at the least, that it is a dictionary as implemented as a part of the NSDictinoary class cluster.

What you can't do is use isKindOfClass: or isMemberOfClass: to determine whether or not a dictionary (or string, array, or set) is mutable. Consider:

NSDictionary *d = [NSDictionary dictionaryWithObject: [[NSObject new] autorelease] forKey: @"Bob"];
NSMutableDictionary *m = [NSMutableDictionary dictionaryWithObject: [[NSObject new] autorelease] forKey: @"Bob"];
NSLog(@"d class: %@ %@ %@", [d class], [d superclass], [[d superclass] superclass]);
NSLog(@"m class: %@ %@ %@", [m class], [m superclass], [[m superclass] superclass]);

This outputs:

d class: NSCFDictionary NSMutableDictionary NSDictionary
m class: NSCFDictionary NSMutableDictionary NSDictionary

d and m are both instances of NSCFDictionary which inherits from NSMutableDictionary (which inherits from NSDictionary).

like image 198
bbum Avatar answered Sep 17 '22 17:09

bbum