我最近一直在为我的一个应用程序使用 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/