xcode - 核心数据产生分析器警告

标签 xcode ios core-data clang-static-analyzer

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

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

+ (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/

相关文章:

ios - 初始化结构

ios - 整个数据从数据库中删除

ios - xcode:更改文件路径后缺少文件xcdatamodeld

swift - 在 XCode、iOS 开发中编译时 Keychain.swift 中生成错误

node.js - 长期使用的前端项目框架中出现巨大的 Node/NPM 崩溃

iphone - iOS导航栏中的下拉列表

ios - Firebase 删除用户问题

ios - 核心数据获取请求的复杂排序

ios - Pod 安装错误 : [! ]无法添加 url 为 `https://github.com/CocoaPods/Specs.git` 的源名为 master

ios - "This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes...."