{
Items = {
Item = {
text = "item 1";
}
Item = {
text = "item 2";
}
Item = {
text = "item 3";
}
}
}
上面是我的 NSDictionary 的一个例子。请记住下一个片段。
NSDictionary *dict = {the afore mentioned dictionary is here}
int count = [[[dict objectForKey:@"Items"] allKeys] count]; // Breakpoint here
NSLog(@"%i", count); // Breakpoint here
在我的断点中,我得到了以下 count
- 在断点 1 处:
count = (int) 128037184
(或其他一些随机大整数) - 在断点 2 上:
count = (int) 5
我本以为两次的整数都是 3。怎么回事?
最佳答案
在第一种情况下,您在执行该行之前中断,因此 count
将拥有恰好位于堆栈该位置的任何随机数据。如果您在调试器中越过该指令,您会看到 count
更改为更合理的数字。
在第二种情况下,我不确定为什么你有比预期更多的键,但简单的做法是只记录整个字典并查看那里有什么:
NSLog(@"%@", dict);
这将向您显示字典中的所有键和值,这可以解释您得到的数字。
关于ios - NSDictionary 长度计数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9100524/