Ios NSDictionary 数组 - 分组值和键

Posted

技术标签:

【中文标题】Ios NSDictionary 数组 - 分组值和键【英文标题】:Ios NSDictionary array - grouping values and keys 【发布时间】:2013-09-14 06:46:13 【问题描述】:

我有以下数组NSDictionary的结果

Bath =     
    Keynsham =         (
        "nsham companies"
    );
;


Bath =     
    "Midsomer Norton" =         (
        "Keynsham companies"
    );
;


Bath =     
    "Norton Radstock" =         (
        "Keynsham taxi companies"
    );
;


Birmingham =     
    "Acock's Green" =         (
        "Acock's Green taxi companies"
    );
;


Birmingham =     
    "Alcester Lane's End" =         (
        "Alcester Lane's End taxi companies"
    );
;

如何组合值和键,以便我最终只得到一个类别,如下所示;

Bath =     
    "Norton Radstock" =         (
        "Keynsham taxi companies"
    );
 "Midsomer Norton" =         (
        "Keynsham companies"
    );

   Keynsham =         (
        "nsham companies"
    );

;

我不确定这是否是最好的解释方式 代码如下

//分配/初始化所有 Nssarrays

  NSURL *url=[NSURL URLWithString:@"http://y.php"];
        NSData *data= [NSData dataWithContentsOfURL:url];

        NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:Nil];
        //instantiate arrays to hold data

        NSMutableDictionary *dictArray=[[NSMutableDictionary alloc]init];
        NSArray *cityName=[[NSArray alloc]init];
        NSArray *townName=[[NSArray alloc]init];
        NSArray *taxis=[[NSArray alloc]init];  

        NSArray *ids=[[NSArray alloc]init];

        for (int i=0; i<json.count; i++)
        

            //cityName=[[NSMutableArray alloc] initWithCapacity:json.count];

           ids = [[json objectAtIndex:i] objectForKey:@"id"];
           cityName = [[json objectAtIndex:i] objectForKey:@"cityName"];
           townName=[[json objectAtIndex:i] objectForKey:@"townName"];

           taxis=[[json objectAtIndex:i] objectForKey:@"taxis"];



        NSMutableArray  *taxisArray=[[NSMutableArray  alloc] initWithObjects:taxis,nil];
       NSMutableDictionary *towensdict=[[ NSMutableDictionary alloc]  initWithObjectsAndKeys:taxisArray,townName, nil];


       NSMutableDictionary *cities1=[[NSMutableDictionary alloc] initWithObjectsAndKeys:towensdict,cityName, nil];


NSLOG (@"%@", cities1) here, gives me the print out above


            [dictArray addEntriesFromDictionary:cities1 ];



Then I tried Jdodgers solution as follows;
   NSMutableDictionary *combinedDictionary = [[NSMutableDictionary alloc] init];
            for (NSDictionary *currentDictionary in dictArray) 
                NSArray *keys = [currentDictionary allKeys];
                 for (int n=0;n<[keys count];n++) 
                  NSMutableDictionary *dictionaryToAdd = [combinedDictionary valueForKey:[keys objectAtIndex:n]];
                if (!dictionaryToAdd) dictionaryToAdd = [[NSMutableDictionary alloc] init];
               [dictionaryToAdd setValuesForKeysWithDictionary:[currentDictionary valueForKey:[keys objectAtIndex:n]]];
               [combinedDictionary setValue:dictionaryToAdd forKey:[keys objectAtIndex:n]];

                NSLog(@"%@", currentDictionary);
                
             

//这给出错误“无法识别的选择器发送到实例”,这是打印输出

combinedDictionary  NSMutableDictionary *   0x000000010012e580
currentDictionary   NSDictionary *const 0x0000000100116460
dictArray   NSMutableDictionary *   0x000000010012e220
[0] key/value pair  
key id  0x0000000100116460
[0] id  
value   id  0x000000010012e440
[0] id  
keys    NSArray *   0x0000000000000000

【问题讨论】:

【参考方案1】:

您可以创建一个 NSMutableDictionary 并循环遍历您的数组,使用 allKeys 将键添加到可变字典中。

例如,如果您的数组名为dictArray,您可以这样做:

NSMutableDictionary *combinedDictionary = [[NSMutableDictionary alloc] init];
for (NSDictionary *currentDictionary in dictArray) 
    NSArray *keys = [currentDictionary allKeys];
    for (int n=0;n<[keys count];n++) 
        NSMutableDictionary *dictionaryToAdd = [combinedDictionary valueForKey:[keys objectAtIndex:n]];
        if (!dictionaryToAdd) dictionaryToAdd = [[NSMutableDictionary alloc] init];
        [dictionaryToAdd setValuesForKeysWithDictionary:[currentDictionary valueForKey:[keys objectAtIndex:n]]];
        [combinedDictionary setValue:dictionaryToAdd forKey:[keys objectAtIndex:n]];
    

此代码首先创建一个字典combinedDictionary,这将是您的最终字典。它遍历数组中的所有字典,并为每个字典执行以下操作:

首先,它获取字典中所有键的数组。对于您提供的字典,该数组在前 3 个中看起来像 @[@"Bath"],在其他两个中看起来像 @[@"Birmingam"]

然后代码循环遍历这些键,并从该键的组合字典中获取已经存在的字典。如果字典不存在,则创建一个。

然后,它从数组中添加字典中的所有值,并将新字典设置为combinedDictionary 中的字典。

【讨论】:

谢谢,但我不断收到无法识别的选择器发送到实例 我添加了更多细节,因为我不断收到无法识别的选择器错误 你的 dictArray 应该是一个数组而不是字典。它是您正在组合的字典数组(例如,它可以包含您的五个示例字典,每个字典都作为数组的一个对象)。

以上是关于Ios NSDictionary 数组 - 分组值和键的主要内容,如果未能解决你的问题,请参考以下文章

iOS:寻找 NSDictionary 值时出现错误:[__NSCFNumber isEqualToString:]: unrecognized selector sent to instanc

ios - 从 NSDictionary 获取值

iOS - 数组与字典(NSArray & NSDictionary)

无法从 NSDictionary 获取值是 ios

从 NSDictionary 数组中获取具有值的数组

检查 NSDictionary 中的键 - 不使用布尔 NSNumber 作为值 @iOS8