【发布时间】:2012-03-20 21:43:15
【问题描述】:
我想用 ARC 创建一个 Singleton,
this is the answer I see。
有没有在不使用块的情况下将此代码转换为类似的代码?
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[MyClass alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
编辑:
我只看到这种方法:
static MyClass *sharedMyClassInstance = nil;
+(MyClass *) sharedMyClass
{
@synchronized(self) {
if (sharedMyClassInstance == nil) {
sharedMyClassInstance = [[self alloc] init];
}
return sharedMyClassInstance;
}
}
这会阻止创建多个对象吗?
【问题讨论】:
标签: singleton automatic-ref-counting objective-c-blocks