如何实现与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屋!
查看全文