Update/Edit coreData managed object

13,262

Have you tried:

[itemToUpdate setValue:[NSNumber numberWithBool:NO] forKey:@"purchased"]

form?

I always subclass NSManagedObject and the dot notation works for declared properties. But you might try this "older" notation to see if that works.

Share:
13,262

Related videos on Youtube

lostincode
Author by

lostincode

Updated on June 04, 2022

Comments

  • lostincode
    lostincode almost 2 years

    I'm trying to edit a CoreData object when a user clicks on a cell in a UITableView based on the cell.accessoryType to show if the item has been clicked. Here is the current code.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSManagedObject *itemToUpdate = [groceryArray objectAtIndex:indexPath.row];
    NSLog(@"updating: %@", itemToUpdate);
    
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        itemToUpdate.purchased = NO;
    }else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        itemToUpdate.purchased = YES;
    }
    
    // Commit the change.
    NSError *error;
    if (![managedObjectContext save:&error]) {
        // Handle the error.
        NSLog(@"Saving changes failed: %@", error);
    
    }
    }
    

    It seems to be selecting the right object because the NSLog() will show the correct item but when I try to update using the dot notation e.g. "itemToUpdate.purchased = YES;" the compiler throws an error "request for member 'purchased' in something not a structure or union".

    I know I'm probably doing this wrong (my first project in xcode) - any advice would be greatly appreciated!

    Thanks

  • lostincode
    lostincode over 13 years
    Thanks! That worked... I got a lot of reading to do. Appreciate your time and help. :)

Related