0

I'd like to sava data (text) if return is clicked on the keyboard. But it's not working. I'd like to display the text, which was saved in a table view cell and it should be also available after the next start of the app.

My code:

    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
}

Log:

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}])
3
  • What isn't working? Why are you passing nil to the save function rather than giving it an error object and examining it to see what, if anything, has gone wrong? Commented Oct 19, 2014 at 9:50
  • I updated the question with my crash log. Commented Oct 19, 2014 at 10:08
  • Look at the last line, where it says "reason=...". It's all very well pasting the crashlog, but it helps if you read it as well. Commented Oct 19, 2014 at 10:10

1 Answer 1

3

The last line of your crash log tells you what has gone wrong.

You've changed your model since running the app. You either need to enable auto migration so that such changes are corrected automatically, or just delete the app from the simulator and run it again.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this works now. But I get a new error in this line of code:var newNote = Model(entity: en!, insertIntoManagedObjectContext: context) is says: fatal error: unexpectedly found nil while unwrapping an Optional value
Which is why explicitly unwrapping optionals is a bad idea, you lose all the type safety built into Swift. There is a problem with something in the line that assigns the entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.