2014-10-19 48 views
0

如果在鍵盤上單擊返回,我想要sava數據(文本)。但它不起作用。 我想要顯示保存在表格視圖單元格中的文本,並且在應用程序的下一次啓動後它也應該可用。使用Swift核心數據保存數據

我的代碼:

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}]) 
+0

什麼不起作用?你爲什麼將'nil'傳遞給'save'函數,而不是給它一個錯誤對象,並檢查它看看是否有錯誤? – Abizern 2014-10-19 09:50:22

+0

我用我的崩潰日誌更新了問題。 – user3576580 2014-10-19 10:08:36

+0

看看最後一行,它說「理由= ...」。這一切都很好地粘貼crashlog,但它也有幫助,如果你閱讀它。 – Abizern 2014-10-19 10:10:51

回答

3

你的崩潰日誌的最後一行告訴你出了什麼問題。

自從運行該應用以來,您已經更改了您的模型。您需要啓用自動遷移功能,以便自動更正這些更改,或者只需從模擬器中刪除該應用程序並再次運行。

+0

是的,現在可以使用。但是我在這行代碼中得到了一個新錯誤:'var newNote = Model(entity:en !, insertIntoManagedObjectContext:context)'說:'致命錯誤:意外地發現nil,同時展開可選值' – user3576580 2014-10-19 10:26:47

+0

這就是爲什麼顯式展開optionals是一個壞主意,你失去了Swift內置的所有類型安全。分配實體的行中存在問題。 – Abizern 2014-10-19 13:25:04