如何在 iphone 中获取 NSMutableDictionary 计数? [英] How to get NSMutableDictionary count in iphone?
问题描述
我想在 iphone 中获得 NSMutableDictionary 计数.我想知道 NSMutableDictionry 中有多少项.我尝试了这些代码来找出解决方案,但对我帮助不大.
I want to get NSMutableDictionary count in iphone. I want to know how many items are in NSMutableDictionry. I tried these code to find out the solution but, not helped me lot.
NSLog(@"Count : %d", [mutableDictionary count]);
它总是返回'0'.如何在 iPhone 中获取 NSMutableDictionary 的数量?提前致谢.
It is always returns '0'. How to get the count of NSMutableDictionary in iPhone? Thanks in advance.
推荐答案
你可以像这样找出有多少键-对象(键-值)对:
You can find out how many key-object (key-value) pairs there are like so:
NSArray * allKeys = [mutableDictionary allKeys];
NSLog(@"Count : %d", [allKeys count]);
编辑
查看字典文档后,NSDictionary
的 count
方法(或属性)应该也能工作.我认为您可能已经收到 0 计数,因为字典是空的或为零.我提供了我的解决方案,因为我更关心枚举键而不是直接计算条目.
Upon looking through the dictionary documentation, NSDictionary
's count
method (or property) should work as well. I think you may have been receiving 0 count because the dictionary was empty or nil. I offered my solution because I tend to care more about enumerating the keys than counting the entries directly.
请考虑您已在其他地方解决了该问题.
• 通过实际填充字典
或
• 通过修复 mutableDictionary 以某种方式为零的错误
Please consider the fact that you fixed the issue somewhere else.
• By actually populating the dictionary
or
• By fixing a bug where mutableDictionary was somehow nil
我运行此测试代码并获得注释输出
I run this test code and get the commented output
NSMutableDictionary * countDict = [NSMutableDictionary dictionaryWithObject:@"test" forKey:@"test"];
[countDict setObject:@"foo" forKey:@"bar"];
NSLog(@"test count %d", countDict.count); //test count 2
countDict = nil;
NSLog(@"test count %d", countDict.count); //test count 0
这篇关于如何在 iphone 中获取 NSMutableDictionary 计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!