ios - NSDictionary 长度计数错误

标签 ios 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

在我的断点中,我得到了以下 count

的值
  • 在断点 1 处:count = (int) 128037184(或其他一些随机大整数)
  • 在断点 2 上:count = (int) 5

我本以为两次的整数都是 3。怎么回事?

最佳答案

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

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

NSLog(@"%@", dict);

这将向您显示字典中的所有键和值,这可以解释您得到的数字。

关于ios - NSDictionary 长度计数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9100524/

相关文章:

java - 有人可以帮助我如何使用 bridgeKit( objective-c 到 Java 代码)..?

ios - UISplitViewController 的主细节 Controller 的动画可见性

ios - 如何在 ios 中使用 NSDictionary 将两个 NSArray 对象显示为 UITableview 的部分

ios - 在 UITableView 问题中搜索字典值和键并显示

ios - NSSortDescriptor 不按字母顺序排序

ios - 使用键更新字典值

ios - 我如何访问和编辑一个对象深入另一个对象 (swift)

ios - 核心数据和 GCD : Passing the correct managed object context to custom NSManagedObjects

ios - 多个 UINavigationBar 中的一个 UIBarButtonItem 实例

iOS:一次设置多个按钮的透明度