如何获取NSMutableDictionary计数在iphone? [英] 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。如何获得计数NSMutableDictionary在iPhone?提前感谢。
It is always returns '0'. How to get the count of NSMutableDictionary in iPhone? Thanks in advance.
推荐答案
您可以了解有多少键对象(键值)对: / p>
You can find out how many key-object (key-value) pairs there are like so:
NSArray * allKeys = [mutableDictionary allKeys];
NSLog(@"Count : %d", [allKeys count]);
EDIT
文档, 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
这篇关于如何获取NSMutableDictionary计数在iphone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!