我想从“Entry”中检索单个值(String)。我相信我错过了一些东西。有人能说出来吗?
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]);
}
顺便说一句,NSLog结果输出如下:
results <NSManagedObject: 0x3d2d360> (entity: Entry; id: 0x3d13650 <x-coredata://6EA12ADA-8C0B-477F-801C-B44FE6E6C91C/Entry/p3> ; data: <fault>)
答案 0 :(得分:1)
NSFetchRequest将返回与请求匹配的NSManagedObjects数组。这与你所看到的一致。假设您要获取名称,可以NSLog(@"the name is %@", [[results objectAtIndex:0] name]);
打印出名称。