ios - NSEntityDescription 仅在 iPad Air 7.1 上返回 nil

标签 ios ipad core-data swift nsentitydescription

我最近一直在为我的一个应用程序使用 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,而在第四行我的应用程序崩溃了。

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

非常感谢任何帮助或建议

最佳答案

我想我遇到了和你一样的问题。我发现第二次访问实体时会崩溃。感谢这篇文章,我能够解决这个问题: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)

关于ios - NSEntityDescription 仅在 iPad Air 7.1 上返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917982/

相关文章:

iphone - 纵向应用程序 - 横向模态视图 - 无自动旋转

javascript - 点击/单击选择输入/文本框字段的内容

iphone - 无法单击 UIScrollView 的任何部分来滚动图像。我只能点击一个特定的部分

ios - 如何在 IBAction 中使用 Core Data 插入数据

iOS App Store 收据文件不存在

iphone - 如何通过Get方法从Web服务中获取数据并想保存到iPhone SDK中的Mutable Dictionary中

django - 当从 iPad 接收 JSON Ajax 请求时,如何使用 Django 表单?

ios - 多个获取请求导致应用程序卡住

core-data - 立即启用文件NSManagedObjectContext的保存?

ios - 从一个函数引用另一个函数中的常量