1

在 Apple 的 BubbleLevel 示例的 AppDelegate 中,他们执行以下操作:

+ (void)initialize {
    if ([self class] == [LevelAppDelegate class]) {
        // Register a default value for the instrument calibration. 
        // This will be used if the user hasn't calibrated the instrument.
        NSNumber *defaultCalibrationOffset = [NSNumber numberWithFloat:0.0];
        NSDictionary *resourceDict = [NSDictionary dictionaryWithObject:defaultCalibrationOffset forKey:BubbleLevelCalibrationOffsetKey];
        [[NSUserDefaults standardUserDefaults] registerDefaults:resourceDict];
    }
}

他们为什么这样做 [self class] == [LevelAppDelegate class] ?

4

1 回答 1

3

如果在加载子类时调用 initialize,此测试可确保初始化代码无效。

http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-initialize

于 2009-05-06T10:40:55.147 回答