this class is not key value coding-compliant for the key itemname

426 views Asked by At

What I am trying to do is take the text that is in my uitextfield called editItemField and write the value in the NSString called toDoItemName to my core data model with the attribute named itemname.

Im getting the above error. My core data model has one attribute named itemname I am assuming think this maybe means I am doing something with an object from an NSArray but I cant do that because its not part of the managedObjectContext?

error

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key itemname.'

code

- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];

if (self.editItemField) {
    // Update existing device
    [self.toDoItemName setValue:self.editItemField.text forKey:@"itemname"];
}
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

prepareforsegue

Im passing the item from the uitableview through this prepareforsegue. its populating my uitextfield on the new viewcontroller. the intention is to edit the item and then update the core data model with the new value.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"EditItemSegue"])
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    Item *item = [[self fetchedResultsController]objectAtIndexPath:indexPath];
    [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
    [segue.destinationViewController setToDoItemName:[item valueForKey:@"itemname"]];

}
}

edit

Can someone tell me if this looks correct in sending the managedObjectContext?

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"EditItemSegue"]) {
    EditItem *item = [segue destinationViewController];
    item.managedObjectContext = _managedObjectContext;
}
}

updated segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"EditItemSegue"])
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    Item *item = [[self fetchedResultsController]objectAtIndexPath:indexPath];
    EditItem *destination = (EditItem *)segue.destinationViewController;
    destination.managedObjectContext=self.managedObjectContext;
    destination.toDoItem = item;
}
}

[NSManagedObject rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x7fc223f5d350 2015-06-11 23:10:31.717 LittleToDoApp[95329:10363535] *** Terminating app due to uncaught exception NSInvalidArgumentException, reason: -[NSManagedObject rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x7fc223f5d350

Ive never seen this error. Is this because it doesnt know where to send this to in the EditItem view? This happens of course when I tap on an item in the uitableview.

1

There are 1 answers

9
Paulw11 On BEST ANSWER

You need to send the NSManagedObject instance to your next view controller, not just a single String

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"EditItemSegue"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        Item *item = [[self fetchedResultsController]objectAtIndexPath:indexPath];
        NextVCClass *destination = (NextVCClass *)segues.destinationViewController;
        destination.managedObjectContext=self.managedObjectContext;
        destination.toDoItem=item; 
    }
}

You need to change NextVCClass to the class of your destination UIViewController subclass