gpt4 book ai didi

objective-c - 核心数据 : keypath name not found in entity

转载 作者:太空狗 更新时间:2023-10-30 04:01:39 27 4
gpt4 key购买 nike

这条消息让我崩溃了:

'NSInvalidArgumentException', reason: 'keypath name not found in entity

显然我没有正确查询我的实体。

//fetching Data

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Viewer" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSString *attributeName = @"dF";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@",attributeName];
[fetchRequest setPredicate:predicate];

NSLog(@"predicate : %@",predicate);
NSError *error;
NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"items : %@",items);

[fetchRequest release];

//end of fetch

这是我的数据模型:

alt text

我想返回“dF”的值,不应该这样调用吗? :

NSString *attributeName = @"dF";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@",attributeName];

最佳答案

如果你想从你的 dF 属性中获取值,你必须获取一个 NSManagedObjects 数组,然后使用 [fetchedManagedObject valueForKey:@"dF"]; 来获得你的值(value)。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Viewer" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

NSManagedObject *mo = [items objectAtIndex:0]; // assuming that array is not empty
id value = [mo valueForKey:@"dF"];

谓词用于获取满足您的条件的 NSManagedObjects 数组。例如。如果您的 dF 是一个数字,您可以创建像“dF > 100”这样的谓词,然后您的提取请求将返回一个包含 NSManagedObjects 的数组将具有大于 100 的 dF 值。但是如果您只想获取值,则不需要任何谓词。

关于objective-c - 核心数据 : keypath name not found in entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3717149/

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