lostincode
lostincode

Reputation: 39

Update/Edit coreData managed object

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

Upvotes: 4

Views: 13302

Answers (2)

Chosia
Chosia

Reputation: 11

I suppose you created a custom subclass of 'NSManagedObject' with 'purchased' as one of the properties. Declare 'itemToUpdate' as an object of this subclass, rather than NSManagedObject:

YourCustomSubclassOfNSManagedObject *itemToUpdate = [groceryArray objectAtIndex:indexPath.row];

Upvotes: 0

westsider
westsider

Reputation: 4985

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.

Upvotes: 6

Related Questions