Master data: key name not found in object

I crash with this post:

'NSInvalidArgumentException', reason: 'key string name not found in object

Obvisouly I am not accessing my object correctly.

//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 

And here is my Model data: alt text

I want to return the value "dF", shouldn't I call it that?

 NSString *attributeName = @"dF"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@",attributeName]; 
+4
source share
1 answer

If you want to get the value from your dF property, you need to get an array of NSManagedObjects and then use [fetchedManagedObject valueForKey:@"dF"]; to get your 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"]; 

Predicates are used to get an array of NSManagedObjects that matches your criteria. For instance. if your dF is a number, you can create a predicate like " dF > 100 ", then your select query will return an array with NSManagedObjects that will have dF values ​​that are> 100. But if you want to get only values, you don't need a predicate.

+7
source

All Articles