gpt4 book ai didi

NSDictionary 中循环的 iOS 问题

转载 作者:行者123 更新时间:2023-11-28 19:55:47 26 4
gpt4 key购买 nike

我有这样的数组:

(
{
code = "+39";
country = "Italy (+39)";
},
{
code = "+93";
country = "Afghanistan(+93)";
},
)

我试图像这样描述 here 在循环中获取元素

所以有我的代码:

int i = 0;
for (id key in self.codes) {
NSDictionary *value = [self.codes objectForKey:key];
if(row == i)
{
return [value objectForKey:@"country"];
}
i++;
}

我有这个错误:

2014-10-22 15:49:56.791 Surfree[1097:60b] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x15387e00
2014-10-22 15:49:56.793 Surfree[1097:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x15387e00'
*** First throw call stack:
(0x2e385ecb 0x38b20ce7 0x2e3897f7 0x2e3880f7 0x2e2d7058 0xe51af 0x30e27bf9 0x30e2ae81 0x3112984b 0x30cea179 0x30c913db 0x30cd5c8f 0x3107ec81 0x31128d7b 0x30e29bcf 0x30e29a85 0x30e2a7eb 0x30e27159 0x30e2724f 0x30bb6d17 0x30bb6a85 0x30bb63ed 0x2ececd33 0x30bb6271 0x30bc2ffd 0x30bc2a73 0x30d8c165 0x30bb6d17 0x30bb6a85 0x30bb63ed 0x2ececd33 0x30bb6271 0x30bc2ffd 0x30bc2a73 0x30d8b979 0x30bc90b5 0x30d8b1cd 0x30d483b1 0x30c6546f 0x30c65279 0x30c65211 0x30bb72e5 0x3083331b 0x3082eb3f 0x3082e9d1 0x3082e3e5 0x3082e1f7 0x30bba99f 0x2e350faf 0x2e350477 0x2e34ec67 0x2e2b9729 0x2e2b950b 0x332286d3 0x30c1a871 0xe6bd9 0x3901eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我该如何解决?为什么我有错误?

当我进行调试时,我在这一行中看到了错误:

NSDictionary *value = [self.codes objectForKey:key];

最佳答案

看起来self.codes是一个数组;你不能像字典一样访问它。使用您的代码的正确方法是这样的:

int i = 0;
for (NSDictionary *value in self.codes) {
if(row == i)
{
return [value objectForKey:@"country"];
}
i++;
}

但是,看起来您实际上只是在尝试访问此内容:

self.codes[row][@"country"]

关于NSDictionary 中循环的 iOS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26509065/

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