【发布时间】:2017-03-13 10:07:17
【问题描述】:
嘿,我一直在线上收到此 BAD Access
try managedContext.save()
代码运行完美,直到它尝试保存我的价值。我认为这可能是因为我使用的是实体的“副本”而不是实际的实体。但这是必要的,因为您不能在代码中实际编辑原始模型。所以我所要做的就是在尝试“.save()”时停止这种不良访问,并且我很好。它位于代码的最后 4 行。
所以基本上在使用时尝试 managedContext.save()。我创建了一个会打印错误的捕获。所以我把“!” “try”的符号,这样我就可以看到应用程序抛出错误的原因,这就是我得到的
致命错误:“试试!”表达式意外引发错误:Error Domain=NSCocoaErrorDomain Code=134020“用于打开商店的模型配置与用于创建商店的模型配置不兼容。”
代码:
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let managedContext: NSManagedObjectContext = appDelegate.managedObjectContext
var properties0 = Array<NSAttributeDescription>()
let playersList0 = NSEntityDescription.entityForName("PlayersList1", inManagedObjectContext: managedContext)
let copy = playersList0!.copy() as! NSEntityDescription
let contentTypeAttribute0 = NSAttributeDescription()
contentTypeAttribute0.name = "firstName"
contentTypeAttribute0.attributeValueClassName = "firstName"
contentTypeAttribute0.attributeType = .StringAttributeType
contentTypeAttribute0.optional = true
properties0.append(contentTypeAttribute0)
copy.properties = properties0
let playerslistCopyto = NSManagedObject(entity: copy, insertIntoManagedObjectContext: managedContext)
playerslistCopyto.setValue("John", forKey: "firstName")
do {
try managedContext.save()
} catch {
print("Error") //prints Error everytime
}
这每次都会打印“错误”作为捕获。我基本上只需要找出如何保存“副本”
【问题讨论】: