views:

69

answers:

1

In the AppDelegate of the BubbleLevel example from Apple they do something like this:

+ (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];
    }
}

Why do they do that [self class] == [LevelAppDelegate class] ?

+3  A: 

This test ensures that the initialization code has no effect if initialize is invoked when a subclass is loaded.

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

oxigen