¿Por qué debería verificar [self class] == [LevelAppDelegate class] en un método de inicialización?

StackOverflow https://stackoverflow.com/questions/828787

  •  06-07-2019
  •  | 
  •  

Pregunta

En el ejemplo AppDelegate del BubbleLevel de Apple, hacen algo como esto:

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

¿Por qué hacen eso [self class] == [LevelAppDelegate class]?

¿Fue útil?

Solución

Esta prueba asegura que el código de inicialización no tenga efecto si se invoca initialize cuando se carga una subclase.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top