- (NSManagedObjectContext *)anObjectByEntityForName:(NSString *)entityName withValue:(NSObject *)value forKeyPath:(NSString *)keyPath {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:@"%@ == %@", keyPath, value]];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
[request release];
[mutableFetchResults release];
return nil;
}
if ([mutableFetchResults count] == 0) {
[request release];
[mutableFetchResults release];
return nil;
}
id anObject = [mutableFetchResults objectAtIndex:0];
[request release];
[mutableFetchResults release];
return anObject;
}
对于键路径“isSelected”和值@YES,此代码返回 nil。但如果没有谓词,则返回所有对象。在数据库中至少有 1 个符合条件的对象。whit有什么问题?