ios - 接收 EXC_BAD_ACCESS 异常

标签 ios swift core-data

我正在使用单例类从 CoreData 中选择数据,并将其发送回调用 ViewController。我的问题是,当获取 ManagedObject 的属性之一时,应用程序崩溃并出现 EXC_BAD_ACCESS 异常。

这似乎只发生在 iOS 9.x 或模拟器上,但在这些系统上非常一致。它没有发生在运行 10.x 的设备上。我将方案诊断设置为显示僵尸对象,现在出现以下错误:

-[CFString copy]: message sent to deallocated instance 0x15b92990

问题是被引用的字符串在我收到此错误之前直接检索到的对象上,并且我使用的是 Swift(因此没有手动释放任何东西),所以我不明白为什么它被释放。

选择对象的代码如下所示:

func getModelTypePrice(mmCode: String, year: Int) -> ModelTypePrice? {
    let request = NSFetchRequest<ModelTypePrice>(entityName: "ModelTypePrice")
    request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [NSPredicate(format: "mmcode = %@", mmCode),
                                                                        NSPredicate(format: "reg_year = %d", year)])
    do {
        let prices = try managedContext.fetch(request)
        if prices.count == 1 {
            return prices[0]
        }
    } catch {
        print("Error selecting object: \(error)")
    }
    return nil
}

从 ViewController 调用,使用如下:

if let price = LibraryAPI.sharedInstance.getModelTypePrice(mmCode: "123", year: 2017) {
    self.newPrice = price.new_price // Error happens here.
}

ViewController 确实有一个名为 newPrice 的可选字符串属性。 ModelTypePrice 的 new_price 属性也是一个可选的字符串。 我在这里有点不知所措,所以任何意见或建议将不胜感激。

最佳答案

这修复了它:[CFNumber release]: message sent to deallocated instance

问题是托管对象的属性名称以 new 开头(它是 new_price)。将其更改为 price_new 修复了它。显然他们改变了 iOS 10.x 中的处理方式,因为这从来都不是问题。

也许这可以让其他人避免一些挫败感。

关于ios - 接收 EXC_BAD_ACCESS 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44521372/

相关文章:

ios - 如何使用 swift 3 为核心数据生成虚拟数据

ios 7 dequeueReusableCellWithIdentifier :forIndexPath method - does it need registerClass method

iphone - 未设置反向关系(在 KVO 处理程序中)

ios - 如何为自定义表格 View 单元动态设置约束?

swift - 如何判断一个对象的类型?

将 C 结构实例复制到 Swift 后缺少成员

ios - UILabel 中文本之前和之后的行

ios - 如何创建Core Data数据库swift 4

ios - 在 Storyboard中添加 subview

ios - 3D Touch Peek - 背景模糊颜色