Mobile/iOS
[iOS]Singleton 사용하기
out of coding
2016. 3. 25. 13:01
Objective-C에서 singleton 사용하는 방법이다.
+ (ExamObject *)getInstance {
static ExamObject* instance = nil;
if(instance == nil) {
@synchronized (self) {
if(instance == nil) {
instance = [[ExamObject alloc] init];
}
}
}
return instance;
}
여기에서 참조할 부분은 static ExamObject* instance = nil; 부분인데,
처음 진입시 한번 nil로 초기화를 시키고, 다음 런타임에서는 해당 부분을 거치지 않게 된다.
재밌네요.