Fetch Data using predicate. Retrieve single value.

Posted by Mr. McPepperNuts on Stack Overflow See other posts from Stack Overflow or by Mr. McPepperNuts
Published on 2010-06-02T18:31:48Z Indexed on 2010/06/02 18:34 UTC
Read the original article Hit count: 263

Filed under:
|
XYZAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", entryToSearchFor]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext]; 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];   

[request setSortDescriptors:sortDescriptors];
[request setEntity: entity]; 
[request setPredicate: predicate]; 

NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];

if (results == nil) {
    NSLog(@"No results found");     
}else {
    NSLog(@"entryToSearchFor %@", entryToSearchFor);
    NSLog(@"results %@", [results objectAtIndex:0]);

}

I want to retrieve a single value (String) from "Entry." I believe I must be missing. Can someone point it out?

Btw, the NSLog results outputs the following:

results <NSManagedObject: 0x3d2d360> (entity: Entry; id: 0x3d13650 <x-coredata://6EA12ADA-8C0B-477F-801C-B44FE6E6C91C/Entry/p3> ; data: <fault>)

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c