更新/编辑coreData管理对象 [英] Update/Edit coreData managed object
问题描述
当用户点击UITableView中的一个单元格,基于cell.accessoryType来显示该项目是否被点击时,我正在尝试编辑一个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;
}
//提交更改。
NSError * error;
if(![managedObjectContext save:& error]){
//处理错误。
NSLog(@保存更改失败:%@,错误);
}
}
似乎是选择正确对象,因为NSLog()将显示正确的项目,但是当我尝试使用点标记更新例如itemToUpdate.purchased = YES;编译器抛出一个错误请求成员购买的东西不是一个结构或联合。
我知道我可能做错了我的第一个项目
感谢
您尝试过:
[itemToUpdate setValue:[NSNumber numberWithBool:NO] forKey:@purchased]
$ b $ p 我总是子类NSManagedObject和点符号工程为宣告物业。但你可以尝试这个旧的符号,看看是否有效。
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
解决方案 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.
这篇关于更新/编辑coreData管理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!