objective-c 책에서 싱글 톤 예제를 보았습니다. 그러나 objective-c와 다른 langs 사이에 'singleton'정의의 의미가 다른지 나는 모른다. 이 [[SingletonClass alloc] init]을 사용하여 새 객체를 만들 수 있습니까? 그렇다면 메모리에 오직 하나의 객체가 있음을 보장하는 방법은 무엇입니까?객관적인 c의 싱글 톤
#import "SingletonClass.h"
@implementation SingletonClass
static SingletonClass *sharedInstance = nil;
// Get the shared instance and create it if necessary.
+ (SingletonClass*)sharedInstance {
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
// We can still have a regular init method, that will get called the first time the Singleton is used.
- (id)init
{
self = [super init];
if (self) {
// Work your initialising magic here as you normally would
}
return self;
}
가능한 중복 : 목표 - C : 정적 필드와 구현 싱글 톤 패턴 (http://stackoverflow.com/questions/6912703/objective-c-static-field-and-implemented-singleton-pattern/6913036 # 6913036) –