Why should I check [self class] == [LevelAppDelegate class] in the initialization method?

In the AppDelegate example from BubbleLevel 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 are they doing this [self class] == [LevelAppDelegate class]?

+1


a source to share


1 answer


This test ensures that the initialization code has no effect if the initialization is called when the 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

+3


a source







All Articles