Objective-C: Getting the True Class of Classes in Class Clusters

Posted by TechZen on Stack Overflow See other posts from Stack Overflow or by TechZen
Published on 2010-05-15T17:07:33Z Indexed on 2010/05/15 17:14 UTC
Read the original article Hit count: 282

Recently while trying to answer a questions here, I ran some test code to see how Xcode/gdb reported the class of instances in class clusters. (see below) In the past, I've expected to see something like:

PrivateClusterClass:PublicSuperClass:NSObject

Such as this (which still returns as expected):

NSPathStore2:NSString:NSObject

... for a string created with +[NSString pathWithComponents:].

However, with NSSet and subclass the following code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    NSSet *s=[NSSet setWithObject:@"setWithObject"];
    NSMutableSet *m=[NSMutableSet setWithCapacity:1];
    [m addObject:@"Added String"];
    NSMutableSet *n = [[NSMutableSet alloc] initWithCapacity:1];
    [self showSuperClasses:s];
    [self showSuperClasses:m];
    [self showSuperClasses:n];
    [self showSuperClasses:@"Steve"];   
}

- (void) showSuperClasses:(id) anObject{
    Class cl = [anObject class];
    NSString *classDescription = [cl description];
    while ([cl superclass]) 
    {
        cl = [cl superclass];
        classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    }
    NSLog(@"%@ classes=%@",[anObject class], classDescription);
}

... outputs:

// NSSet *s
NSCFSet classes=NSCFSet:NSMutableSet:NSSet:NSObject
//NSMutableSet *m
NSCFSet classes=NSCFSet:NSMutableSet:NSSet:NSObject
//NSMutableSet *n
NSCFSet classes=NSCFSet:NSMutableSet:NSSet:NSObject
// NSString @"Steve"
NSCFString classes=NSCFString:NSMutableString:NSString:NSObject

The debugger shows the same class for all Set instances. I know that in the past the Set class cluster did not return like this.

  1. What has changed? (I suspect it is a change in the bridge from Core Foundation.)
  2. What class cluster report just a generic class e.g. NSCFSet and which report an actual subclass e.g. NSPathStore2?
  3. Most importantly, when debugging how do you determine the actual class of a NSSet cluster instance?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone