拉出内部NSMutableDictionary iOS的valueForKeys

时间:2013-02-08 14:42:33

标签: ios ios5 nsdictionary nsmutabledictionary key-value-coding

好的,我将Json提要存储到一个名为jsonArray的数组中。然后我循环遍历jsonArray拉出数组键并将它们存储为字符串。然后我将这些字符串添加到名为innerDict的内部字典中,然后使用下面的密钥thePostCode将该字典添加到信息字典中。所以基本上innerDict存储在infoDict中。

-(void)pointInfo{

infoDict = [[NSMutableDictionary alloc]init];

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

innerDict = [[NSMutableDictionary alloc]init];

info = [[jsonArray objectAtIndex:i]objectForKey:@"inf"];
thePostCode = [[jsonArray objectAtIndex:i]objectForKey:@"pc"];
mail = [[jsonArray objectAtIndex:i]objectForKey:@"mail"];
url = [[jsonArray objectAtIndex:i]objectForKey:@"url"];
type = [[jsonArray objectAtIndex:i]objectForKey:@"items"];

[innerDict setObject:type forKey:@"Items"];
[innerDict setObject:info forKey:@"Info"];
[innerDict setObject:mail forKey:@"Mail"];
[innerDict setObject:url forKey:@"Url"];

[infoDict setObject:innerDict forKey:thePostCode];

}

infoDict的输出如下所示:

infoDict is {
"ME14 4NN" =     {
    Info = "";
    Items = 4;
    Mail = "";
    Url = "";
};
"ME15 6LG" =     {
    Info = af;
    Items = "0,6,9";
    Mail = "";
    Url = "";
};
"ME15 6YE" =     {
    Info = "";
    Items = "4,5,6,7,11";
    Mail = "";
    Url = "";
};
}

现在我要做的是获取innerDict对象的值,即上面的“ME15 6YE”,并将其用作查询,以获取Info,Items,Mail和Url键的关联数据。我一直盯着我的屏幕看几个小时,但我只是没有得到它。

我可以使用下面的方法拉出内部字典的最后一个对象但是我想获取与特定邮政编码相关的所有值,这些邮政编码将是innerDict密钥。此刻完全被大脑炸掉了!

for (NSMutableDictionary *dictionary in infoDict) {

    NSLog(@"URL %@", [innerDict objectForKey:@"Url"]);
    NSLog(@"MAIL %@", [innerDict objectForKey:@"Mail"]);
    NSLog(@"ITEMS %@", [innerDict objectForKey:@"Items"]);
    NSLog(@"ITEMS %@", [innerDict objectForKey:@"Info"]);

}

2 个答案:

答案 0 :(得分:1)

要获取正确的内部词典,请使用objectForKey

NSDictionary *innerDict = [infoDict objectForKey:@"ME15 6YE"];
NSLog(@"Url %@", [innerDict objectForKey:@"Url"]);
NSLog(@"Mail %@", [innerDict objectForKey:@"Mail"]);
NSLog(@"Items %@", [innerDict objectForKey:@"Items"]);
NSLog(@"Info %@", [innerDict objectForKey:@"Info"]);

答案 1 :(得分:1)

也许我误解了一些事情,但我认为这应该回答你的问题。

您在for循环中使用字典,但尝试通过 innerDict 访问它。将其更改为:

for (NSMutableDictionary *dictionary in infoDict) {
    NSLog(@"URL %@", [dictionary objectForKey:@"Url"]);
    NSLog(@"MAIL %@", [dictionary objectForKey:@"Mail"]);
    NSLog(@"ITEMS %@", [dictionary objectForKey:@"Items"]);
    NSLog(@"ITEMS %@", [dictionary objectForKey:@"Info"]);
}

或者,对于一个人,

NSMutableDictionary *inner = [infoDict objectForKey:@"POST CODE HERE"];
NSLog(@"URL %@", [inner objectForKey:@"Url"]);
NSLog(@"MAIL %@", [inner objectForKey:@"Mail"]);
NSLog(@"ITEMS %@", [inner objectForKey:@"Items"]);
NSLog(@"ITEMS %@", [inner objectForKey:@"Info"]);