I'm working on a simple notes app, and I created an entity called "Note" in the data model that have only one attribute that named "content" of type nsstring.
Now I careated a Note class using editor/create NSManagedObject subclass from the data model section.
In my create notes view controller i'm saving the data to the database in the prepareForSegue method that looks like this:
@implementation NOCreateNotesViewController
- (NSManagedObjectContext *) managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.saveButton) return;
if (self.noteText.text.length > 0) {
self.note = [[Note alloc] init];
self.note.content = self.noteText.text;
}
NSManagedObjectContext * context = [self managedObjectContext];
// creating a new managed object
NSManagedObject *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context];
[newNote setValue:self.noteText.text forKey:@"content"];
NSError * error = nil;
if ([context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
But not I want to pass the object that a user wants to edit (meaning the note in the selected row from the table view) this way:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"editeNote"]) {
NSManagedObject * selectedNote = [self.notes objectAtIndex:[self.tableView indexPathForSelectedRow].row];
NOCreateNotesViewController * destination = [segue destinationViewController];
destination.note = selectedNote;
}
}
And obviously I can't pass a Note object with a pointer to NSManagedObject...this is because I want to update my Note entity if this is an editing segue instead of creating a new one.
In the tutorial I learned core data basics from they teach with just creating objects on the fly and note using a custom class for the objects with editor/create NSManagedObject subclass so i'm getting a bit confused..
Please help