2010-09-15 81 views
4

我這個消息崩潰未發現:核心數據:的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 

這裏是我的數據模型: alt text

我想回到「DF」的價值,不應該這樣稱呼它? :

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

謂詞中的'name'是什麼?你的實體沒有屬性'name'! – vfn 2010-09-15 11:32:16

+0

啊,那就是我很困惑的地方。我想返回名稱爲「dF」的屬性的值。 – 2010-09-15 11:39:18

回答

7

如果你想從你的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