我创建了一个名为Statutory
的实体,其中有四列name
,complianceName
,statMappingName
,country
我希望获得与特定complianceName
匹配的所有statMappingName
个名称。以下是我的代码
NSString *nameToGet = self.statNameArray[indexPath.row] ;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"complianceName = %@", nameToGet];
[fetch setPredicate:predicate];
NSLog(@"n %@",predicate);
NSError *error = nil;
NSArray *results = [[self managedObjectContext] executeFetchRequest:fetch error:&error];
if(results) {
NSLog(@"Entities with that name: %@", results);
} else {
NSLog(@"Error: %@", error);
}
但它没有提供特定于statMappingName
的合规性名称。如何才能获得具有特定complianceName
的所有statMappingName
?
答案 0 :(得分:1)
如果您要从core data
您可以使用以下方法: -
NSEntityDescription *entity = [NSEntityDescription entityForName:entityname inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setReturnsObjectsAsFaults:NO];
[request setEntity:entity];
[request setFetchLimit:1];
[request setPredicate:[NSPredicate predicateWithFormat:@"(complianceName LIKE %@)",nametoGet]];
NSError *error = nil;
NSMutableArray *tempDataArr = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
注意: -
* entityname:您的实体名称
* self.managedObjectContext:您的managedobjectcontext
获取所有合规性名称使用以下方法
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:entityName];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSMutableArray *tempDataArr =[NSMutableArray new];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(ANY complianceName LIKE %@)",nametoGet]];
tempDataArr = [[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
答案 1 :(得分:1)
试试这个: -
NSString *firstName = statMappingName;
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"firstName == %@", firstName]];
答案 2 :(得分:1)
尝试使用包含statMappingName
的这些代码NSPredicate *predicate = [NSPredicate predicateWithFormat:@"complianceName contains[c] %@", statMappingName];