NSDictionary 长度计数错误
{
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
在我的断点中,我在断点 1 上得到以下 count
值
- :
count = (int) 128037184
(或其他一些随机和大整数) - 在断点 2 上:
count = (int) 5
我预计两次的 int 都是 3。这是怎么回事?
{
Items = {
Item = {
text = "item 1";
}
Item = {
text = "item 2";
}
Item = {
text = "item 3";
}
}
}
Above is an example of my NSDictionary. Keep it in mind for this next snippet.
NSDictionary *dict = {the afore mentioned dictionary is here}
int count = [[[dict objectForKey:@"Items"] allKeys] count]; // Breakpoint here
NSLog(@"%i", count); // Breakpoint here
In my breakpoints, I get the following values for count
- On breakpoint 1:
count = (int) 128037184
(or some other random and big int) - On breakpoint 2:
count = (int) 5
I would have expected an int of 3 both times. What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一种情况下,您会在执行该行之前中断,因此
count
将拥有堆栈上该位置恰好存在的任何随机数据。如果您在调试器中跳过该指令,您将看到count
更改为更合理的数字。在第二种情况下,我不确定为什么你的键比你预期的要多,但简单的做法就是记录整个字典并查看其中有什么:
这将向你显示字典,它可以解释你得到的数字。
In the first case, you're breaking before the line is executed, so
count
will have whatever random data happens to be in that spot on the stack. If you step over that instruction in the debugger, you'll seecount
change to a more reasonable number.In the second case, I'm not sure why you have more keys than you expect, but the simple thing to do would be to just log the entire dictionary and see what's there:
That'll show you all the keys and values in the dictionary, which may explain the number you're getting.
我使用 [dict count] 给出 NSDictionary 中对象的数量。
I use [dict count] which gives you count of objects in NSDictionary.