【发布时间】:2013-02-03 01:32:17
【问题描述】:
我正在尝试获取线程安全的单例设计模式的更新版本。 Here 是我知道的一个版本。但是,我无法使其在 iOS6 中运行
这是我想要做的:
这是我的类方法
+(id)getSingleton
{
static dispatch_once_t pred;
static EntryContainerSingleton *entriesSingleton = nil;
dispatch_once(&pred, ^{
entriesSingleton = [[super alloc] init];
});
return entriesSingleton;
}
+(id)alloc
{
@synchronized([EntryContainerSingleton class])
{
NSLog(@"inside alloc of EntryContainerSingleton");
ERROR >>>>> NSAssert(entriesSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
ERROR >>>>> entriesSingleton = [super alloc];
ERROR >>>>> return entriesSingleton;
}
return nil;
}
-(id)init
{
self = [super init];
......Some custom INitialization
return self;
}
此代码引发如上标记的错误。错误消息显示使用未声明的标识符。另外上面的链接推荐使用
[[allocWithZone:nil] init]
当我这样使用它时它会抱怨
+(id)allocWithZone:(NSZone*)zone
{
return [self instance];
}
经过数小时的努力使其发挥作用。如果有人能指出如何正确地做到这一点,那就太好了。我花了很多时间在谷歌上搜索,但没有找到完整的实现示例。
谢谢
【问题讨论】:
-
看看this answer...它在iOS5和iOS6中也运行良好。
-
请注意,在您绝对不能有两个实例的情况下,该问题的公认答案在某种程度上是合理的,即使尝试也会出错。这在 ObjC 中是非常罕见的情况。在几乎所有情况下,问题中的代码(不是答案)都是正确的。
-
@holex 谢谢,但您的链接指的是可能不是线程安全的实现。我正在寻找我在问题中提供的 GCD 代码 sn-p 的正确版本。
-
这里的问题中给出了正确的版本,并解释了为什么它是正确的:stackoverflow.com/questions/9119042/…。你不应该覆盖
alloc,除非你有非常非常强烈的理由这样做。 -
也在这里:mikeash.com/pyblog/…
标签: ios objective-c design-patterns thread-safety singleton