嘿,我在线路上总是遇到这种错误的访问
try managedContext.save()
Run Code Online (Sandbox Code Playgroud)
代码完美运行,直到它尝试保存我的值。我认为这可能是因为我使用的是实体的“副本”而不是实际的实体。但这是必要的,因为您无法在代码中实际编辑原始模型。所以我所要做的就是在尝试“.save()”时停止这种错误的访问,我很好。它位于代码的最后 4 行。
所以基本上在使用时尝试 ManagedContext.save()。我创建了一个将打印错误的捕获。所以我把“!” “try”中的符号,以便我可以了解应用程序抛出错误的原因,这就是我得到的
致命错误:“尝试!” 表达式意外引发错误:错误域 = NSCocoaErrorDomain 代码 = 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() …Run Code Online (Sandbox Code Playgroud)