2012-05-14 4 views
1

dinpatch_once_t 및 _sharedObject가 sharedInstance를 반복적으로 호출 할 때 0 및 nil로 설정되지 않은 이유를 이해하려고합니다. 그것은 코딩 된 방식으로, 정적 변수를 재설정 할 수 있기 때문에 로컬 변수가 다시 초기화되는 것 같습니다. ARC 또는 iOS 메모리 관리의 기본 기능은 무엇입니까?iOS의 싱글 톤

+ (id)sharedInstance 
{ 
// structure used to test whether the block has completed or not 
static dispatch_once_t p = 0; 

// initialize sharedObject as nil (first call only) 
__strong static id _sharedObject = nil; 

// executes a block object once and only once for the lifetime of an application 
dispatch_once(&p, ^{ 
    _sharedObject = [[self alloc] init]; 
}); 

// returns the same object each time 
return _sharedObject; 
} 

답변

7

실제로는 ARC 또는 iOS가 아닌 C가됩니다. 이것은 "내부 정적 변수"(a.k.a 로컬 정적 변수)이며 해당 선언은 한 번만 처리됩니다. 함수의 범위이지만 확장 된 수명을가집니다.