2

我有以下代码,它使用 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
}
4

2 回答 2

1

您还可以objectID使用如下谓词获取和检索对象

[NSPredicate predicateWithFormat:@"objectID IN %@", objectIDs]
于 2013-01-11T00:13:11.373 回答
0

您可以使用 [NSManagedObject setValuesForKeysWithDictionary:]

更多信息:https ://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html

于 2013-01-10T19:57:15.217 回答