Cocoa 使用cwlsynthesisingleton.h时内存问题
我正在使用创建一个单例。但在分析Xcode中的源代码时,会显示以下错误:Cocoa 使用cwlsynthesisingleton.h时内存问题,cocoa,singleton,Cocoa,Singleton,我正在使用创建一个单例。但在分析Xcode中的源代码时,会显示以下错误: Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected 在这条线上 CWL_SYNTHESIZE_SINGLETON_FOR_CLASS_WITH_ACCESSOR(MyManager, sharedManager) 我不在这个项目中使用ARC。有什么建议可以解决这个问题吗?它应该忽略这一
Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected
在这条线上
CWL_SYNTHESIZE_SINGLETON_FOR_CLASS_WITH_ACCESSOR(MyManager, sharedManager)
我不在这个项目中使用ARC。有什么建议可以解决这个问题吗?它应该忽略这一点吗?简单的回答是:不要使用该代码,它很旧,不再推荐使用。现在创建单件的正确方法是使用
dispatch\u once
:
+ (instancetype)sharedInstance
{
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [(id)[super alloc] init];
});
return sharedInstance;
}
如果要阻止类的用户直接分配实例,请对不希望调用的方法使用标头中的unavailable
compiler属性:
+ (instancetype)alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
- (instancetype)init __attribute__((unavailable("init not available, call sharedInstance instead")));
+ (instancetype)new __attribute__((unavailable("new not available, call sharedInstance instead")));