根据NSDictionary键值将NSArray拆分为子数组 [英] Split NSArray into sub-arrays based on NSDictionary key values

查看:108
本文介绍了根据NSDictionary键值将NSArray拆分为子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序调用SOAP Web服务并检索一长串XML,然后该应用程序解析为 NSArray NSDictionary 对象。 NSArray 包含Rental Apartment信息列表,每个信息都存储在 NSDictionary 中。



整个清单可能包含10种不同类型的公寓(即2房,3房),我们需要拆分 NSArray 基于Room-Type进入较小的 NSArray ,其在 NSDictionary 对象中具有键roomType。 / p>

目前我们的算法是


  1. 使用 [NSArray valueForKeyPath :@@ distinctUnionofObjects.room-type]
    获取唯一房间类型值的列表。

  2. 遍历唯一列表房间类型值

  3. 对于每个唯一的房间类型值,使用 NSPredicate 从原始列表中检索匹配的项目

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

  NSArray * arrOriginal = ...... ...; //包含解析的XML列表

NSMutableArray * marrApartmentsByRoomType = [NSMutableArray arrayWithCapacity:10];

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

。对于(* NSString的在strRoomType arrRoomTypes){
NSPredicate * predicateRoomType = [NSPredicate predicateWithFormat:@ roomType =%@,strRoomType];

NSArray * arrApartmentsThatMatchRoomType = [arrOriginal filteredArrayUsingPredicate:predicateRoomType]; //每循环一次循环

[marrApartmentsByRoomType addObject:arrApartmentsThatMatchRoomType];
}

然而,由于原始列表可能包含大量内容,因此第3步需要很长时间金额(> 100,000)的物品。似乎 NSPredicate 遍历每个键值的整个列表。是否有更有效的方法将大型 NSArray 拆分为较小的 NSArray ,基于 NSDictionary 键?

解决方案

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

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

NSMutableArray * tmp = [groups objectForKey:key];
if(tmp == nil){
tmp = [[NSMutableArray alloc] init];
[分组setObject:tmp forKey:key];
}
[tmp addObject:dict];
}
NSMutableArray * marrApartmentsByRoomType = [groups allValues];


We have an app that calls a SOAP web service and retrieves a long list of XML, which the app then parses into an NSArray of NSDictionary objects. The NSArray contains a list of Rental Apartment information, each of which is stored into an NSDictionary.

The entire list may contain 10 different types of Apartments (i.e. 2-room, 3-room), and we need to split the NSArray into smaller NSArrays based on Room-Type, which has the key "roomType" in the NSDictionary objects.

Currently our algorithm is

  1. Use [NSArray valueForKeyPath:@"@distinctUnionofObjects.room-type"] to obtain a list of unique room-type values.
  2. Loop through the list of unique room-type values
  3. For each unique room-type value, use NSPredicate to retrieve matching items from the Original list

Our code is below (renamed for clarity):

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];
}

However, step 3 is taking a long time as the original list may contain large amount (>100,000) of items. It seems that NSPredicate goes through the entire list for each key value. Is there a more efficient way of splitting a large NSArray into smaller NSArrays, based on NSDictionary keys?

解决方案

If the order of your splited Arrays is not important, i have a solution for you:

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

这篇关于根据NSDictionary键值将NSArray拆分为子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆