如何实现与 ARC 兼容的 Objective-C 单例? [英] How do I implement an Objective-C singleton that is compatible with ARC?
本文介绍了如何实现与 ARC 兼容的 Objective-C 单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Xcode 4.2 中使用自动引用计数 (ARC) 时,如何转换(或创建)一个能够正确编译和运行的单例类?
How do I convert (or create) a singleton class that compiles and behaves correctly when using automatic reference counting (ARC) in Xcode 4.2?
推荐答案
与您(应该)已经这样做的方式完全相同:
In exactly the same way that you (should) have been doing it already:
+ (instancetype)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;
}
这篇关于如何实现与 ARC 兼容的 Objective-C 单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文