4

我们有一个应用程序调用 SOAP Web 服务并检索一长串 XML,然后应用程序将其解析为NSArray对象NSDictionary。包含一个出租公寓信息列表,NSArray每个信息都存储在一个NSDictionary.

整个列表可能包含 10 种不同类型的 Apartments(即 2-room、3-room),我们需要根据 Room-Type 将它们拆分NSArray成更小的 s,它在对象中具有键“roomType”。NSArrayNSDictionary

目前我们的算法是

  1. 用于[NSArray valueForKeyPath:@"@distinctUnionofObjects.room-type"] 获取唯一房间类型值的列表。
  2. 循环遍历唯一房间类型值的列表
  3. 对于每个唯一的房间类型值,用于NSPredicate从原始列表中检索匹配的项目

我们的代码如下(为清楚起见重命名):

NSArray *arrOriginal = ... ...; // Contains the Parsed XML list

NSMutableArray *marrApartmentsByRoomType = [NSMutableArray arrayWithCapacity:10];

NSMutableArray *arrRoomTypes = [arrOriginal valueForKeyPath:@"distinctUnionOfObjects.roomType"];

for(NSString *strRoomType in arrRoomTypes) {
  NSPredicate *predicateRoomType = [NSPredicate predicateWithFormat:@"roomType=%@", strRoomType];

  NSArray *arrApartmentsThatMatchRoomType = [arrOriginal filteredArrayUsingPredicate:predicateRoomType];  // TAKES A LONG TIME EACH LOOP-ROUND

  [marrApartmentsByRoomType addObject:arrApartmentsThatMatchRoomType];
}

但是,第 3 步需要很长时间,因为原始列表可能包含大量(>100,000)项。似乎NSPredicate遍历每个键值的整个列表。有没有一种更有效的方法可以根据键将大的拆分NSArray成较小的 s ?NSArrayNSDictionary

4

3 回答 3

3

如果拆分数组的顺序不重要,我有一个解决方案:

NSArray *arrOriginal;
NSMutableDictionary *grouped = [[NSMutableDictionary alloc] initWithCapacity:arrOriginal.count];
for (NSDictionary *dict in arrOriginal) {
    id key = [dict valueForKey:@"roomType"];

    NSMutableArray *tmp = [grouped objectForKey:key];
    if (tmp == nil) {
        tmp = [[NSMutableArray alloc] init];
        [grouped setObject:tmp forKey:key];
    }
    [tmp addObject:dict];
}
NSMutableArray *marrApartmentsByRoomType = [grouped allValues];
于 2013-01-15T10:25:12.687 回答
1

这是相当高效的

- (NSDictionary *)groupObjectsInArray:(NSArray *)array byKey:(id <NSCopying> (^)(id item))keyForItemBlock
{
    NSMutableDictionary *groupedItems = [NSMutableDictionary new];
    for (id item in array) {
        id <NSCopying> key = keyForItemBlock(item);
        NSParameterAssert(key);

        NSMutableArray *arrayForKey = groupedItems[key];
        if (arrayForKey == nil) {
            arrayForKey = [NSMutableArray new];
            groupedItems[key] = arrayForKey;
        }
        [arrayForKey addObject:item];
    }
    return groupedItems;
}
于 2014-03-14T13:32:47.950 回答
0

改进@Jonathan 答案

  1. 将数组转换为字典
  2. 保持与原始数组相同的顺序

    //only to a take unique keys. (key order should be maintained)
    NSMutableArray *aMutableArray = [[NSMutableArray alloc]init];
    
    NSMutableDictionary *dictFromArray = [NSMutableDictionary dictionary];
    
    for (NSDictionary *eachDict in arrOriginal) {
    //Collecting all unique key in order of initial array
    NSString *eachKey = [eachDict objectForKey:@"roomType"];
    if (![aMutableArray containsObject:eachKey]) {
        [aMutableArray addObject:eachKey];
    }
    
    NSMutableArray *tmp = [grouped objectForKey:key];
    tmp  = [dictFromArray objectForKey:eachKey];
    
    if (!tmp) {
        tmp = [NSMutableArray array];
        [dictFromArray setObject:tmp forKey:eachKey];
    }
    [tmp addObject:eachDict];
    
    }
    
    //NSLog(@"dictFromArray %@",dictFromArray);
    //NSLog(@"Unique Keys :: %@",aMutableArray);
    

    //再次从字典转换为数组...

    self.finalArray = [[NSMutableArray alloc]init];
    for (NSString *uniqueKey in aMutableArray) {
       NSDictionary *aUniqueKeyDict = @{@"groupKey":uniqueKey,@"featureValues":[dictFromArray objectForKey:uniqueKey]};
    [self.finalArray addObject:aUniqueKeyDict];
    }
    

希望,当客户希望最终数组与输入数组的顺序相同时,它会有所帮助。

于 2016-07-20T04:43:41.650 回答