NSDictionary 长度计数错误

发布于 2025-01-02 07:28:14 字数 664 浏览 0 评论 0原文

{
    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2025-01-09 07:28:14

在第一种情况下,您会在执行该行之前中断,因此 count 将拥有堆栈上该位置恰好存在的任何随机数据。如果您在调试器中跳过该指令,您将看到 count 更改为更合理的数字。

在第二种情况下,我不确定为什么你的键比你预期的要多,但简单的做法就是记录整个字典并查看其中有什么:

NSLog(@"%@", dict);

这将向你显示字典,它可以解释你得到的数字。

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 see count 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:

NSLog(@"%@", dict);

That'll show you all the keys and values in the dictionary, which may explain the number you're getting.

抱着落日 2025-01-09 07:28:14

我使用 [dict count] 给出 NSDictionary 中对象的数量。

I use [dict count] which gives you count of objects in NSDictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文