Objective-C: getting the true class of the class clusters

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

PrivateClusterClass:PublicSuperClass:NSObject

      

For example, this one (which still returns as expected):

NSPathStore2:NSString:NSObject

      

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

.

However, with NSSet and a 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, a cluster of classes of classes did not return like this.

  • What changed? (I suspect this is a bridge change from the Core Foundation.)
  • In a cluster of a cluster, only the general class is specified, for example. NSCFSet and which report the actual subclass eg. NSPathStore2?
  • Most importantly, when debugging, how do you determine the actual class of the NSSet cluster instance?
+2


a source to share


1 answer


  • The implementation details are most likely related to the CF transition. If you look closely, you can see that CoreFoundation now has an Objective-C support group supporting various Foundation classes.

  • Another implementation detail. The implementation of Foundation class clusters changes, sometimes very, sometimes subtly, with each release of Mac OS X. The exact set of private classes associated with a given implementation will typically depend on any specific set of requirements needed to achieve a flexible and reasonably optimal solution while maintaining public class API contract.

  • NSCFSet is the actual class. The specifics of the instances are determined by how you selected it in the first place. There is no open way to determine if your set is mutable or immutable. This is really on purpose; going down the road of writing a lot of code that changes behavior based on the variability of the cluster of classes is insane.



+3


a source







All Articles