使用Swift保存数据的核心数据

时间:2014-10-19 09:26:34

标签: core-data swift ios8 tableview

如果在键盘上点击了返回,我想要sava数据(文本)。但它不起作用。 我希望显示保存在表格视图单元格中的文本,并且应该在下次启动应用程序后显示。

我的代码:

    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: 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}])

1 个答案:

答案 0 :(得分:3)

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

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