Skip to content

Instantly share code, notes, and snippets.

@gin0606
Created May 7, 2013 15:04
Show Gist options
  • Save gin0606/5533319 to your computer and use it in GitHub Desktop.
Save gin0606/5533319 to your computer and use it in GitHub Desktop.
static __ClassName__ *shared__ClassName__ = nil;
+ (__ClassName__ *)shared__ClassName__ {
@synchronized (self) {
if (shared__ClassName__ == nil) {
[[[self alloc] init] autorelease]; // ここでは代入していない
}
}
return shared__ClassName__;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized (self) {
if (shared__ClassName__ == nil) {
shared__ClassName__ = (__ClassName__ *) [super allocWithZone:zone];
return shared__ClassName__; // 最初の割り当てで代入し、返す
}
}
return nil; // 以降の割り当てではnilを返すようにする
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; // 解放できないオブジェクトであることを示す
}
-(oneway void)release{
// 何もしない
}
- (id)autorelease {
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment