3

我正在对应用程序进行最后的润色,并且我正在摆脱每个编译器/分析器警告。

我有一堆 Class 方法来包装我的应用程序对核心数据实体的访问。这是“挑衅”分析仪。

+ (CDProductEntity*) newProductEntity {

    return (CDProductEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"CDProductEntity" inManagedObjectContext:[self context]];
}

这会导致分析器警告:

具有 +0 保留计数的对象返回给调用者,其中预期 +1(拥有)保留计数

在调用上述类方法的方法中,我有这个:

CDProductEntity *newEntity = [self newProductEntity];

这会导致分析器警告:

方法返回一个具有 +1 保留计数(拥有引用)的 Objective-C 对象

显式释放或自动释放 Core Data 实体通常非常非常糟糕,但这就是它要求我在这里做的吗?首先它告诉我它有一个 +0 保留计数,这很糟糕,然后它告诉我它有一个 +1,这也很糟糕。

What can I do to ensure that I am either dealing with a Analyzer hiccup or that I release correctly?

Thanks in advance

4

1 回答 1

6

The problem static analyzer complains about may be in your method name - per obj-c naming conventions methods with alloc, new or copy in their names are expected to return objects that caller 'owns' and must release - and your method returns autoreleased object.

quote from 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).

So for a start try I'd suggest just removing 'new' from your method name

于 2011-01-04T13:51:56.307 回答