依奥斯的NSDictionary阵列 - 分组值和键 [英] Ios NSDictionary array - grouping values and keys

查看:239
本文介绍了依奥斯的NSDictionary阵列 - 分组值和键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的的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"
    );
};

如何才能让我结束了只有一类,如下图所示我结合值和键;

How can i combine the values and keys so that I end up with only one category as shown below;

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

   Keynsham =         (
        "nsham companies"
    );

};

我不知道这是否是解释它的最好办法
 在code是如下:

I am not sure if this is the best way to explain it the code is as follows

//所有Nssarrays分配/初始化

//all Nssarrays allocated/ initialised

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

//这给了错误无法识别的选择发送到实例,这里是打印出来

//this gives error "unrecognized selector sent to instance", here is the print out

combinedDictionary的NSMutableDictionary * 0x000000010012e580
currentDictionary的NSDictionary * const的0x0000000100116460
dictArray的NSMutableDictionary * 0x000000010012e220
[0]键/值对结果
密钥ID 0x0000000100116460
[0] ID结果
值id 0x000000010012e440
[0] ID结果
NSArray的钥匙* 0x0000000000000000

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

推荐答案

您可以通过您的阵列创建一个NSMutableDictionary和循环利用增加的钥匙可变字典中的 allKeys

You could create an NSMutableDictionary and loop through your array, adding the keys to the mutable dictionary using the allKeys.

例如,如果你的阵列名为 dictArray ,你可以这样做:

For example, if your array was called dictArray, you could do:

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

这code首先创建一个字典 combinedDictionary 这将是你最后的字典。遍历所有的数组中的字典,并为每一个执行以下操作:

This code first creates a dictionary combinedDictionary that will be your final dictionary. It loops through all of the dictionaries in your array and for each one does the following:

首先,它得到在字典中的所有键的阵列。对于词典您提供这个数组会像 @ @浴] 为第3和 @ @伯明翰] 其他两个。

First, it gets an array of all keys in the dictionary. For the dictionaries you provided this array will look like @[@"Bath"] for the first 3 and @[@"Birmingam"] for the other two.

在code再通过这些键循环,并从该键组合的字典得到现有的字典。如果字典不存在,创建一个。

The code then loops through these keys and gets the already existing dictionary from the combined dictionary from this key. If the dictionary doesn't exist, one is created.

然后,将所有来自阵列的字典中的值,并设定新的字典是一个在 combinedDictionary

Then, it adds all of the values from the dictionary from the array and sets the new dictionary to be the one in combinedDictionary.

这篇关于依奥斯的NSDictionary阵列 - 分组值和键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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