NSEntityDescription 仅在 iPad Air 7.1 上返回 nil

Posted

技术标签:

【中文标题】NSEntityDescription 仅在 iPad Air 7.1 上返回 nil【英文标题】:NSEntityDescription returns nil on iPad Air 7.1 only 【发布时间】:2014-07-23 18:18:09 【问题描述】:

我最近一直在为我的应用程序使用 CoreData。奇怪的是,它只在装有 ios 7.x 的 iPad Air 上崩溃。我已经在物理设备和 iOS 模拟器上运行过,它永远不会在 Air 上崩溃,并且总是在其他地方运行。 (在 iPad Air iOS 8.0 上不会崩溃)

崩溃发生在我的一个 CoreData 调用中,特别是

    var childEntity = NSEntityDescription.entityForName(ChildInSessionEntity, inManagedObjectContext: context)
    var parentEntity = NSEntityDescription.entityForName(ParentInSessionEntity, inManagedObjectContext: context)
    var newChild = ChildInSession(entity: childEntity, insertIntoManagedObjectContext: context)
    var newParent = ParentInSession(entity: parentEntity, insertIntoManagedObjectContext: context)

第一行不返回 nil,但第二行返回,第四行我的应用程序崩溃了。

这是一个非常奇怪的场景,因为在其他模拟器上它不返回 nil 值。

非常感谢任何帮助或建议

【问题讨论】:

您使用的是哪个版本的 XCode?这很可能是一个错误,1) 可能已经修复或 2) 您应该向 Apple 报告(在测试期间还存在一些其他 iOS 7.1 问题)。 昨天最新的 xCode-Beta4,对我来说似乎也是一个错误,但我担心我的 childEntity 没有设置为 nil 值 ChildInSessionEntityParentInSessionEntity 是什么?它们是字符串吗? 是的,它们是字符串,我可能没有遵循正确的命名约定,但我已将所有实体字符串保存在单独文件中的变量中 【参考方案1】:

我想我和你有同样的问题。我发现第二次访问实体时它会崩溃。感谢这篇文章,我能够解决这个问题:http://stackanswers.com/questions/24440927/swift-core-data-on-ios7-device-entityforname-on-second-entity-is-nil

出于某种原因,您需要将实体名称作为 NSString 的实例传递,例如

// Rather than...
let entity = NSEntityDescription.entityForName("Entity", inManagedObjectContext: self.managedObjectContext!)

// Use this instead...
let entityName: NSString = "Entity"
let entity = NSEntityDescription.entityForName(entityName, inManagedObjectContext: self.managedObjectContext!)

如果您使用的是获取请求,那么您可以使用快捷方式:

let entityName: NSString = "Entity"
let fetchRequest = NSFetchRequest(entityName)

【讨论】:

【参考方案2】:

我认为这是一个错误,我只有 iphone 5s (iOS 7.1) 有同样的问题。通过选择 .xcdatamodeld 并在“Target Membership”下的“File Inspector”下从项目中删除检查,它意外地工作了。

【讨论】:

我发现这也是一种可能的解决方案,我试过了,(未检查,构建,运行,崩溃)然后再次检查(构建,运行,崩溃),我也不确定如何用 Apple 记录这样的错误 与此同时,我找到了一种解决方法/修复方法,我明确地将我的常量实体名称设为 NSString 类型并且它可以编译和运行,我是 *** 的新手,我应该添加这个作为答案?对我来说仍然感觉像一个错误

以上是关于NSEntityDescription 仅在 iPad Air 7.1 上返回 nil的主要内容,如果未能解决你的问题,请参考以下文章

OCMock - 试图模拟 NSEntityDescription

NSEntityDescription.entity 崩溃应用程序(SIGABRT)

NSEntityDescription 创建方法的区别

获取 NSEntityDescription 关系

NSEntityDescription.insertNewObject 函数导致 Swift 3 崩溃

从“NSEntityDescription *”分配给“NSManagedObject Subclass*”的不兼容指针类型