2

最近在这里尝试回答一个问题时,我运行了一些测试代码,以查看 Xcode/gdb 如何报告类 clusters中的实例类。(见下文)在过去,我预计会看到如下内容:

PrivateClusterClass:PublicSuperClass:NSObject

比如这个(仍然按预期返回):

NSPathStore2:NSString:NSObject

... 对于用 . 创建的字符串+[NSString pathWithComponents:]

但是,使用 NSSet 和子类化以下代码:

- (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);
}

...输出:

// 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

调试器为所有 Set 实例显示相同的类。我知道过去 Set 类集群没有像这样返回。

  1. 发生了什么变化?(我怀疑这是核心基金会的桥梁变化。)
  2. 哪个类集群只报告一个通用类,例如 NSCFSet,而哪个报告一个实际的子类,例如 NSPathStore2?
  3. 最重要的是,在调试时如何确定 NSSet 集群实例的实际类?
4

1 回答 1

3
  1. An implementation detail changed most likely related to the bridging to CF. If you look closely, you'll see that a bunch of the Objective-C implementation backing various Foundation classes can now be found in CoreFoundation.

  2. Another implementation detail. The implementation of the Foundation class clusters change -- sometimes greatly, sometimes subtly -- with each release of Mac OS X. The exact set of private classes related to said implementation is generally fallout from whatever particular set of requirements were needed to achieve a flexible and reasonably optimal solution while also preserving the API contract of the public facing classes.

  3. NSCFSet is the actual class. The features of the instances are determined by how you allocated it in the first place. There is no exposed way to determine if your set is mutable or immutable. This is actually on purpose; going down the path of writing a lot of code that changes behavior based on the mutability of a class cluster leads to madness.

于 2010-05-15T17:38:33.243 回答