Save Object to CoreData

I am using CoreData with the iPhone SDK. I am making a note taking app. I have a table with note objects displayed in my model. When I click the button, I want to save the text in text form to an editable object. How can I do it? I tried a few things, but no one works.

thanks

EDIT:

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity]; NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context]; [newManagedObject setValue:detailViewController.textView.text forKey:@"noteText"]; NSError *error; if (![context save:&error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } 

The code above saves it correctly, but saves it as a new object. I want it to be saved as the one I selected in my TableView.

+7
iphone core-data
source share
1 answer

You should check the Master Data Programming Guide . It’s hard to know exactly what you want from the question, but the main idea:

 -(IBAction)saveNote { //hooked up in Interface Builder (or programmatically) self.currentNote.text = self.textField.text; //assuming currentNote is an NSManagedObject subclass with a property called text, and textField is the UITextField } //later, at a convenient time such as application quit NSError *error = nil; [self.managedObjectContext save:&error]; //saves the context to disk 

EDIT: if you want to edit an existing object, you must get the object from the selected result controller, for example. NSManagedObject *currentObject = [fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]] , then edit this object. I also recommend using your own subclass of NSManagedObject with property declarations, rather than using setValue:forKey , as it is more flexible.

+14
source share

All Articles