Core data 使用Swift保存核心数据

Core data 使用Swift保存核心数据,core-data,swift,ios8,tableview,Core Data,Swift,Ios8,Tableview,如果在键盘上单击return,我想保存数据(文本)。但它不起作用。 我想显示文本,它保存在表格视图单元格中,在下一次启动应用程序后也应该可用 我的代码: func textFieldShouldReturn(textField: UITextField) -> Bool { tableViewData.append(textField.text) textField.text = "" self.tableView.reloadData() tex

如果在键盘上单击return,我想保存数据(文本)。但它不起作用。 我想显示文本,它保存在表格视图单元格中,在下一次启动应用程序后也应该可用

我的代码:

    func textFieldShouldReturn(textField: UITextField) -> Bool {

    tableViewData.append(textField.text)
    textField.text = ""
    self.tableView.reloadData()
    textField.resignFirstResponder()

    // Reference to our app delegate

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

    // Reference moc

    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("note", inManagedObjectContext: contxt)

    // Create instance of pur data model an initialize

    var newNote = Model(entity: en!, insertIntoManagedObjectContext: contxt)

    // Map our properties

    newNote.note = textField.text

    // Save our context

    contxt.save(nil)
    println(newNote)

    return true
}
日志:

CoreData:错误:-addPersistentStoreWithType:SQLite配置:(null)URL:file:///Users/Patti/Library/Developer/CoreSimulator/Devices/A70306D7-76BE-489A-82B3-FBAA9390D5A4/data/Containers/Data/Application/FC51C1B7-416B-463E-B976-C175642A4B34/Documents/Note_App.sqlite 选项:(空)。。。返回错误Domain=NSCOCAERRORDOMAIN Code=134100“操作无法完成。(Cocoa错误134100。)”UserInfo=0x7fa79add8080{metadata={
NSPersistenceFrameworkVersion=519;
NSStoreModelVersionHashes={
附注=;
};
NSStoreModelVersionHashesVersion=3;
NSStoreModelVersionIdentifiers=(
""
);
NSStoreType=SQLite;
NSStoreUUID=“8348BBED-7C75-4EF5-B73C-075E8719696E”;
“_NSAutoVacuumLevel”=2;
},reason=用于打开存储的模型与用于创建存储的模型}与userInfo dictionary不兼容{
元数据={
NSPersistenceFrameworkVersion=519;
NSStoreModelVersionHashes={
附注=;
};
NSStoreModelVersionHashesVersion=3;
NSStoreModelVersionIdentifiers=(
""
);
NSStoreType=SQLite;
NSStoreUUID=“8348BBED-7C75-4EF5-B73C-075E8719696E”;
“_NSAutoVacuumLevel”=2;
};
原因=“用于打开存储的模型与用于创建存储的模型不兼容”;
}
2014-10-19 12:05:19.559注释应用程序[2450:43299]未解决的错误可选(错误域=您的\u错误\u域代码=9999“未能初始化应用程序保存的数据”用户信息=0x7fa79addb450{NSLocalizedFailureReason=创建或加载应用程序保存的数据时出错。NSLocalizedDescription=未能初始化应用程序保存的数据,NSUnderlyingError=0x7fa79add80c0“操作无法完成。(Cocoa错误134100.)”),可选([NSLocalizedFailureReason:创建或加载应用程序保存的数据时出错,NSLocalizedDescription:未能初始化应用程序保存的数据,NSUnderlyingError:error Domain=NSCoCaerorDomain Code=134100“操作无法完成。(Cocoa错误134100)。”UserInfo=0x7fa79add8080{元数据={
NSPersistenceFrameworkVersion=519;
NSStoreModelVersionHashes={
附注=;
};
NSStoreModelVersionHashesVersion=3;
NSStoreModelVersionIdentifiers=(
""
);
NSStoreType=SQLite;
NSStoreUUID=“8348BBED-7C75-4EF5-B73C-075E8719696E”;
“_NSAutoVacuumLevel”=2;
},原因=用于打开存储的模型与用于创建存储的模型不兼容}])

崩溃日志的最后一行告诉您出了什么问题


运行应用程序后,您已更改了模型。您需要启用自动迁移,以便自动更正此类更改,或者从模拟器中删除应用程序,然后再次运行。

崩溃日志的最后一行告诉您出了什么问题


运行应用程序后,您已更改了模型。您需要启用自动迁移,以便自动更正此类更改,或者从模拟器中删除应用程序,然后再次运行。

崩溃日志的最后一行告诉您出了什么问题


运行应用程序后,您已更改了模型。您需要启用自动迁移,以便自动更正此类更改,或者从模拟器中删除应用程序,然后再次运行。

崩溃日志的最后一行告诉您出了什么问题



运行应用程序后,您已更改了模型。您需要启用自动迁移,以便自动更正此类更改,或者只需从模拟器中删除应用程序,然后再次运行。

什么不起作用?为什么要将
nil
传递到
保存
功能,而不是给它一个错误对象并检查它如果有什么不对劲的话,看看有什么不对吗?我用我的崩溃日志更新了这个问题。看看最后一行,上面写着“reason=…”。粘贴crashlog很好,但如果你也阅读它,它会有所帮助。什么不起作用?为什么你要将
nil
传递给
save
函数,而不是给它一个错误对象并检查它,看看有什么地方出错了?我用我的崩溃日志更新了这个问题。看最后一行,它说“原因=。。。“。粘贴crashlog很好,但如果你也阅读它,它会有所帮助。什么不起作用?为什么你要将
nil
传递给
save
函数,而不是给它一个错误对象,并检查它,看看有什么地方出了问题?我用我的崩溃日志更新了这个问题。看最后一行,它说“原因=。。。“。粘贴crashlog很好,但如果你也阅读它,它会有所帮助。什么不起作用?为什么你要将
nil
传递给
save
函数,而不是给它一个错误对象,并检查它,看看有什么地方出了问题?我用我的崩溃日志更新了这个问题。看最后一行,它说“reason=…”。粘贴crashlog很好,但是如果您也阅读它,它会有所帮助。是的,现在可以了。但是我在这行代码中发现了一个新错误:
var newNote=Model(entity:en!,insertIntoManagedObjectContext:context)
是这样说的:
致命错误:在展开可选值时意外发现nil
这就是为什么显式展开可选值是一个坏主意,您将失去Swift中内置的所有类型安全性。分配实体的行中存在问题。是的,现在可以工作了。但这行代码中出现了一个新错误:
var newNote=Model(entity:en!,insertIntoManagedObjectContext:context)
表示:
致命错误:在展开可选值时意外发现nil
,该值为
CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/Patti/Library/Developer/CoreSimulator/Devices/A70306D7-76BE-489A-82B3-FBAA9390D5A4/data/Containers/Data/Application/FC51C1B7-416B-463E-B976-C175642A4B34/Documents/Note_App.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7fa79add8080 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes =     {
    Notes = <674765e4 00e077d5 e3b4d2ca 2795eee2 42850989 4a2a825a 0c289097 387aa3a5>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =     (
    ""
);
NSStoreType = SQLite;
NSStoreUUID = "8348BBED-7C75-4EF5-B73C-075E8719696E";
"_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to      create the store} with userInfo dictionary {
metadata =     {
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =         {
        Notes = <674765e4 00e077d5 e3b4d2ca 2795eee2 42850989 4a2a825a 0c289097 387aa3a5>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =         (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "8348BBED-7C75-4EF5-B73C-075E8719696E";
    "_NSAutoVacuumLevel" = 2;
};
reason = "The model used to open the store is incompatible with the one used to  create the store";
}
2014-10-19 12:05:19.559 Note App[2450:43299] Unresolved error Optional(Error   Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data"    UserInfo=0x7fa79addb450 {NSLocalizedFailureReason=There was an error creating or loading  the application's saved data., NSLocalizedDescription=Failed to initialize the  application's saved data, NSUnderlyingError=0x7fa79add80c0 "The operation couldn’t be  completed. (Cocoa error 134100.)"}), Optional([NSLocalizedFailureReason: There was an  error creating or loading the application's saved data., NSLocalizedDescription: Failed   to initialize the application's saved data, NSUnderlyingError: Error  Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error  134100.)" UserInfo=0x7fa79add8080 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes =     {
    Notes = <674765e4 00e077d5 e3b4d2ca 2795eee2 42850989 4a2a825a 0c289097  387aa3a5>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =     (
    ""
);
NSStoreType = SQLite;
NSStoreUUID = "8348BBED-7C75-4EF5-B73C-075E8719696E";
"_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to  create the store}])