2013-04-24 60 views
1

我使用CWLSynthesizeSingleton.h来创建单例。但在Xcode分析源代码时,它显示了这个错误:在这条线使用CWLSynthesizeSingleton.h时的内存问题

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 

。任何建议如何解决这个问题?它应该忽略它吗?

回答

0

简单的答案:不要使用该代码,它是旧的,不再推荐。创建单身这几天正确的方法是使用dispatch_once

+ (instancetype)sharedInstance 
{ 
    static dispatch_once_t once; 
    static id sharedInstance; 
    dispatch_once(&once, ^{ 
     sharedInstance = [(id)[super alloc] init]; 
    }); 
    return sharedInstance; 
} 

如果你想阻止你的类的用户直接分配一个实例,然后使用unavailable编译器的属性,在标题为这些方法你不不想叫:

+ (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")));