초기화 메소드에서 [self class] == [levelAppDelegate 클래스]를 확인 해야하는 이유는 무엇입니까?

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

  •  06-07-2019
  •  | 
  •  

문제

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 클래스]를하는 이유는 무엇입니까?

도움이 되었습니까?

해결책

이 테스트는 서브 클래스가로드 될 때 초기화가 호출되면 초기화 코드가 영향을 미치지 않도록합니다.

http://developer.apple.com/ippone/library/documentation/cocoa/reference/foundation/classes/nsobject_class/reference/reference.html#//apple_ref/doc/uid/20000050-initialize

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top