티스토리 뷰
-- XCode 4.2 부터 ARC 가 추가되어 좋은건 써야 겠기에
http://iphone.galloway.me.uk/iphone-sdktutorials/singleton-classes/ 참고
.m 에서는 아래 코드와 같이(TWEngine 을 자기 클래스 이름으로 ㅇ_ㅇ.)
.h 에서는 NSCopying 프로토콜 추가 해야 될듯(선언은 +(TWEngine*)sharedTWEngine 만)
static TWEngine *sharedTWEngineManager = nil;
+(TWEngine*)sharedTWEngine {
@synchronized(self) {
if (sharedTWEngineManager == nil)
sharedTWEngineManager = [[self alloc] init];
}
return sharedTWEngineManager;
}
+(id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedTWEngineManager == nil) {
sharedTWEngineManager = [super allocWithZone:zone];
return sharedTWEngineManager;
}
}
return nil;
}
-(id)copyWithZone:(NSZone *)zone {
return self;
}
-(id)retain {
return self;
}
-(unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
-(void)release {
//do nothing
}
-(id)autorelease {
return self;
}
사용할땐 TWEngine *myEngine = [TWEngine sharedTWEngine];
참고 :
http://blog.hyperjeff.net/code?id=43
http://projectcocoa.com/2009/10/26/objective-c-singleton-class-template/
http://iphone.galloway.me.uk/iphone-sdktutorials/singleton-classes/ 참고
.m 에서는 아래 코드와 같이(TWEngine 을 자기 클래스 이름으로 ㅇ_ㅇ.)
.h 에서는 NSCopying 프로토콜 추가 해야 될듯(선언은 +(TWEngine*)sharedTWEngine 만)
static TWEngine *sharedTWEngineManager = nil;
+(TWEngine*)sharedTWEngine {
@synchronized(self) {
if (sharedTWEngineManager == nil)
sharedTWEngineManager = [[self alloc] init];
}
return sharedTWEngineManager;
}
+(id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedTWEngineManager == nil) {
sharedTWEngineManager = [super allocWithZone:zone];
return sharedTWEngineManager;
}
}
return nil;
}
-(id)copyWithZone:(NSZone *)zone {
return self;
}
-(id)retain {
return self;
}
-(unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
-(void)release {
//do nothing
}
-(id)autorelease {
return self;
}
사용할땐 TWEngine *myEngine = [TWEngine sharedTWEngine];
참고 :
http://blog.hyperjeff.net/code?id=43
http://projectcocoa.com/2009/10/26/objective-c-singleton-class-template/
'나름 프로그래밍? > Objective-C' 카테고리의 다른 글
UIImageView (0) | 2010.04.21 |
---|---|
UIWebView (0) | 2010.04.21 |
5. Views, Drawing, and Animation (January 19, 2010) (0) | 2010.04.09 |
4. Building an Application; Model, View, Controller; Nib Files; Controls and Target-Action (January 14, 2010) (0) | 2010.04.09 |
3. Custom Classes, Object Lifecycle, Autorelease, Objective-C Properties (January 12, 2010) (0) | 2010.04.08 |