我正在尝试计算NSDictionary
词典的词典中词典键的数量,但是空了。我要计算的值是字典SubDict
的字典中有多少个字典,在每个字典Dict1
,Dict2
,Dict3
中它们是字典theDict
的一部分-在示例中低于此值应总计9
。我如何正确计算儿童词典的键数?
这是我在NSDictionary
结构中使用的代码。
NSDictionary *keyCount = [theDict objectForKey:@"SubDict"];
NSUInteger count = [[keyCount allKeys] count];
NSLog(@"%lu", (unsigned long) count);
它返回值0。
Dict1 = {
SubDict = {
1 = data;
2 = data;
3 = data;
4 = data;
};
};
Dict2 = {
SubDict = {
1 = data;
2 = data;
};
};
Dict3 = {
SubDict = {
1 = data;
2 = data;
3 = data;
};
}; }
最佳答案
如果theDict
看起来像这样,则theDict
中没有“ SubDict”的键。而是有四个子字典,每个子字典都有一个孩子。因此,您需要累计每个的计数:
int count = 0;
for (NSDictionary * dict in theDict.allValues) {
count += [[dict objectForKey:"SubDict"] count];
}
NSLog(@"total items: %d", count);
关于objective-c - 计算子NSDictionary key ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30700868/