5
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?

stevex
  • 5,589
  • 37
  • 52

5 Answers5

20

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

bbum
  • 162,346
  • 23
  • 271
  • 359
15

NSDictionary is a class cluster. Read about them here:

Cocoa Fundamentals Guide

As the "actual" class itself is private, no, it is not safe to depend on this.

If you need to know if your class is really an NSDictionary or not, use [dict isKindOfClass:[NSDictionary class]];

bbum
  • 162,346
  • 23
  • 271
  • 359
GendoIkari
  • 11,734
  • 6
  • 62
  • 104
  • 4
    Good answer. And if the next question is "I'm trying to determine if a dictionary is mutable or not", then ... no ... you can't do that either. – bbum Dec 08 '10 at 18:41
  • 3
    Could you not use `isMemberOfClass:[NSMutableDictionary class]` for that? – GendoIkari Dec 08 '10 at 18:42
  • Yep, isMemberOfClass: can be used to determine if a dictionary is mutable or not. – JustSid Dec 08 '10 at 18:52
  • This answer has bumped me to exactly 1337 reputation. SWEET. – GendoIkari Dec 08 '10 at 18:55
  • 2
    With the edit, this answer is now wrong. So are the suggestions that you can use `-isMemberOfClass:` to check for mutability. You can't. – bbum Dec 08 '10 at 21:45
  • Furthermore, you *shouldn't* need to check for mutability. If you want a mutable version, make a `mutableCopy`. – dreamlax Dec 08 '10 at 21:46
  • Why can't you use isMemberOfClass to find out if your object is a dictionary or not?? – GendoIkari Dec 08 '10 at 21:49
  • OOPS... I meant isKindOfClass:... updating now. – GendoIkari Dec 08 '10 at 21:52
  • I understand why it won't work to check if it's mutable... I also found this: http://stackoverflow.com/questions/1096772/is-it-safe-to-use-iskindofclass-against-an-nsstring-instance-to-determine-type. But won't it work to check if the object is a dictionary? – GendoIkari Dec 08 '10 at 21:55
  • 1
    Gendolkari: Yes, you can test whether an object is a kind of NSDictionary to test whether it is a dictionary. Don't test whether it's a kind of NSMutableDictionary or any of the private subclasses. – Peter Hosey Dec 08 '10 at 21:59
  • (added a space so I can re-instate the up vote that it deserves now that it has been edited :) – bbum Dec 08 '10 at 22:20
1

So, basically, you need to know if you are having an NSMutableDictionary or a bare NSDictionary beforehand. Or don't need to know. Or create an NSMutableDictionary from your object (NSDictionary or NSMutableDictionary)?

What was the question originally for?

Cyril Godefroy
  • 1,400
  • 12
  • 15
0

For an NSDictionary mutability test, could't you just to a 'respondsToSelector' for a method that only an NSMutableDictionary would have, like addObject:ForKey:?

sambecker
  • 1,099
  • 12
  • 28
0

If you want to test for mutability, your best bet would probably be conformsToProtocol: @protocol(NSMutableCopying)

Mike C.
  • 601
  • 3
  • 8