Core data 更新/编辑coreData托管对象

Core data 更新/编辑coreData托管对象,core-data,ios,Core Data,Ios,当用户基于cell.accessoryType在UITableView中单击某个单元格时,我试图编辑CoreData对象,以显示该项是否已被单击。这是当前的代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSMa

当用户基于cell.accessoryType在UITableView中单击某个单元格时,我试图编辑CoreData对象,以显示该项是否已被单击。这是当前的代码

- (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);

}
}
它似乎选择了正确的对象,因为NSLog()将显示正确的项,但当我尝试使用点符号进行更新时,例如“itemToUpdate.purchased=YES”;编译器抛出一个错误“请求非结构或联合中的成员‘purchased’”

我知道我可能做错了(我在xcode中的第一个项目)-任何建议都将不胜感激

谢谢

您是否尝试过:

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


我总是将NSManagedObject作为子类,点表示法适用于声明的属性。但是您可以尝试使用这种“旧”表示法,看看它是否有效。

我想您创建了一个自定义的“NSManagedObject”子类,其中的一个属性是“purchased”。将“itemToUpdate”声明为此子类的对象,而不是NSManagedObject:

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

谢谢那起作用了。。。我有很多书要读。感谢您的时间和帮助。:)