我正在对应用程序进行最后的润色,我正在摆脱所有编译器/分析器警告。
我有一堆类方法来包装我的应用程序对核心数据实体的访问。这是在“挑衅”分析器。
+ (CDProductEntity*) newProductEntity {
return (CDProductEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"CDProductEntity" inManagedObjectContext:[self context]];
}
这会导致分析器警告:
Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected
在调用上述类方法的方法中我有这个:
CDProductEntity *newEntity = [self newProductEntity];
这会导致分析器警告:
Method returns an Objective-C object with a +1 retain count (owning reference)
显式释放或自动释放一个 Core Data 实体通常是非常非常糟糕的,但这就是它要求我在这里做的吗? 首先它告诉我它的保留计数为 +0,这很糟糕,然后它告诉我它的保留计数为 +1,这也很糟糕。
我该怎么做才能确保我处理的是分析器问题或我正确释放?
提前致谢
最佳答案
静态分析器提示的问题可能出在您的方法名称中——根据 obj-c 命名约定,名称中带有 alloc、new 或 copy 的方法应该返回调用者“拥有”且必须释放的对象——并且您的方法返回自动释放的对象。
引自 docs :
You own any object you create.
You “create” an object using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy).
因此,首先尝试从您的方法名称中删除“new”
关于xcode - 核心数据产生分析器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4594250/