Why is NSDictionary reporting 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 it?

+5
source share
5 answers

NSDictionary is a cluster of classes. Read about them here:

Cocoa Fundamentals Guide

Since the "actual" class itself is closed, no, it is not safe from it.

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

+15
source

NSDictionary- a class cluster, as Gendolkari said, and a class cluster is documented .

, .

, , , , :

[myThingaMaHoover isKindOfClass: [NSDictionary class]];

, , , NSDictinoary.

, , isKindOfClass: isMemberOfClass:, , ( , ) . :

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]);

:

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

d m - NSCFDictionary, NSMutableDictionary ( NSDictionary).

+20

, , , NSMutableDictionary NSDictionary . . NSMutableDictionary (NSDictionary NSMutableDictionary)?

?

+1

, , , . ToProtocol: @protocol (NSMutableCopying)

0
source

For the NSDictionary mutability test, you can simply “answer” to “Select” for a method that will only have NSMutableDictionary, for example addObject: ForKey :?

0
source

All Articles