如何将NSDictionaryResults转换为NSManagedObject,

时间:2013-01-10 16:51:12

标签: ios core-data nsdictionary nsmanagedobject

我有以下代码,使用carparts从数据库中提取汽车。 但是我遇到了重复,所以我启用了具有独特结果的NSDictionaryResultsType(我认为这是摆脱重复的唯一方法)。

现在我相信我有一系列字典而不是Car对象。 如何根据我的结果获取汽车对象?

-(NSArray*) loadCarsFromCoreDataUsingCarParts:(NSMutableArray*)inputCarParts{
    NSLog(@"carParts =%@",inputCarParts);
    NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
    //To find the cars we are using the carParts
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];

    //sets up fetch request details
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:[self parseSearchObjectsIntoAPredicate:inputCarParts:3]];
    [fetchRequest setReturnsDistinctResults:YES];
    [fetchRequest setResultType:NSDictionaryResultType];

    //Perform fetch
    NSError *error;
    NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    return records;//i think this is an array of dictionaries
}

2 个答案:

答案 0 :(得分:1)

您还可以获取objectID并使用类似

的谓词检索对象
[NSPredicate predicateWithFormat:@"objectID IN %@", objectIDs]

答案 1 :(得分:0)