我正在尝试过滤对CD的获取请求,并且最初它工作但现在它返回一个空数组。我检查了实体(Provider),我试图过滤的密钥名称是正确的(providerName)。我已经检查了SQLite数据库和记录,其名称与我正在搜索的providerName相匹配。 (见下面附图)。有人可以审查我的代码,看看我做错了吗?感谢:
- (NSArray* ) filterItemsFromCoreData : (NSArray* ) filters {
//create a managed object context
NSManagedObjectContext* context = [self managedObjectContext];
//create error container and fetch request
NSError* errMsg;
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
//get our filters from passed array
NSString* filterProviderName = [filters objectAtIndex:0];
NSString* filterProviderZip = [filters objectAtIndex:1];
NSString* filterProviderPhone = [filters objectAtIndex:2];
//create predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"providerName == %@", filterProviderName];
//create entity description
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Provider" inManagedObjectContext:context];
//add entity and predicate to fetch request
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
//get the objects
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&errMsg];
NSLog(@"%@", fetchedObjects);
return fetchedObjects;
}
实体屏幕捕获
SQLite DB
答案 0 :(得分:0)
将谓词格式中的格式说明符%@
更改为单引号'%@'
。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"providerName == '%@'", filterProviderName];
这应该会再次返回结果。