gpt4 book ai didi

ios - 拉出内部 NSMutableDictionary iOS 的 valueForKeys

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:09 25 4
gpt4 key购买 nike

好的,我获取 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"]);

}

最佳答案

要获取正确的内部字典,请使用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"]);

关于ios - 拉出内部 NSMutableDictionary iOS 的 valueForKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14774989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com