核心数据:的keyPath名在实体
问题描述:
我这个消息崩溃未发现:核心数据:的keyPath名在实体
“NSInvalidArgumentException”的,理由是:“的keyPath名未找到实体
Obvisouly我不是我的查询实体正确。
//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
这里是我的数据模型:
我想回到“DF”的价值,不应该这样称呼它? :
NSString *attributeName = @"dF";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@",attributeName];
答
如果你想从你的dF
财产中获得价值,你必须获取的NSManagedObjects
数组,然后用[fetchedManagedObject valueForKey:@"dF"];
让你的价值。
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
的数组,其dF值大于100.但如果您只想获取值,则不需要任何谓词。
+0
完美无缺,谢谢! – 2010-09-16 00:52:04
谓词中的'name'是什么?你的实体没有属性'name'! – vfn 2010-09-15 11:32:16
啊,那就是我很困惑的地方。我想返回名称为“dF”的属性的值。 – 2010-09-15 11:39:18