gpt4 book ai didi

objective-c - 为什么我不能更新核心数据实体属性的 boolean 值?

转载 作者:太空狗 更新时间:2023-10-30 03:49:33 24 4
gpt4 key购买 nike

这让我很生气。在 Objective-C 中,我有一个带有 boolean 属性 DeletedItem 实体。我希望能够将 Deleted 的值设置为 YES 或 1。

这是我的代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSString *itemID = [[fetchedObjects objectAtIndex:(int)[currentTable selectedRow]] valueForKey:@"ItemID"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ItemID = %@", itemID];
[fetchRequest setPredicate:predicate];

NSError *error = nil;
Item *objectToDelete = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];
if (objectToDelete == nil) {
NSLog(@"ERROR");
}

[fetchRequest release];

[objectToDelete setValue:[NSNumber numberWithBool:YES] forKey:@"Deleted"];

[managedObjectContext save:&error];

需要注意的是,我能够成功更改不同的属性;例如:我将字符串 -DEL 添加到 Item 的属性 CodeName 的末尾。当我查看数据表时,这些值的字符串会相应更新,但是 Deleted 的值仍然为 0。

最佳答案

我强烈推荐为该属性使用不同的名称。 NSManagedObject 已经有与此名称密切相关的方法(特别是 -isDeleted),这可能会导致与您的自定义属性名称发生冲突。作为the documentation对于 NSPropertyDescription 说:

Note that a property name cannot be the same as any no-parameter method name of NSObject or NSManagedObject. For example, you cannot give a property the name "description". There are hundreds of methods on NSObject which may conflict with property names—and this list can grow without warning from frameworks or other libraries. You should avoid very general words (like "font”, and “color”) and words or phrases which overlap with Cocoa paradigms (such as “isEditing” and “objectSpecifier”).

如果您将属性名称更改为不太可能与任何其他属性/方法名称冲突的名称,您的代码是否有效?

关于objective-c - 为什么我不能更新核心数据实体属性的 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10866852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com