无法从核心数据中获取特定列

时间:2016-05-03 05:49:31

标签: ios objective-c core-data fetch

我创建了一个名为Statutory的实体,其中有四列namecomplianceNamestatMappingNamecountry

我希望获得与特定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

3 个答案:

答案 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];