Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 使用cwlsynthesisingleton.h时内存问题_Cocoa_Singleton - Fatal编程技术网

Cocoa 使用cwlsynthesisingleton.h时内存问题

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。有什么建议可以解决这个问题吗?它应该忽略这一

我正在使用创建一个单例。但在分析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。有什么建议可以解决这个问题吗?它应该忽略这一点吗?

简单的回答是:不要使用该代码,它很旧,不再推荐使用。现在创建单件的正确方法是使用
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")));