如何在iphone中获取NSMutableDictionary计数?

Gop*_*ath 13 iphone count nsmutabledictionary ios

我想在iphone中获得NSMutableDictionary计数.我想知道NSMutableDictionry中有多少项.我尝试了这些代码来找出解决方案,但并没有帮助我.

NSLog(@"Count : %d", [mutableDictionary count]);
Run Code Online (Sandbox Code Playgroud)

它总是返回'0'.如何在iPhone中获取NSMutableDictionary的数量?提前致谢.

Jes*_*ack 31

您可以找出有多少关键对象(键值)对,如下所示:

NSArray * allKeys = [mutableDictionary allKeys];
NSLog(@"Count : %d", [allKeys count]);
Run Code Online (Sandbox Code Playgroud)

编辑

在通过字典文件看,NSDictionarycount方法(或属性)应该工作以及.我想你可能已经收到0计数,因为字典是空的或零.我提供了我的解决方案,因为我倾向于更多地关注枚举密钥而不是直接计算条目.

请考虑您在其他地方修复了问题.
•通过实际填充字典

•修复mutableDictionary以某种方式为零的错误

我运行此测试代码并获得注释输出

  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
Run Code Online (Sandbox Code Playgroud)


Ank*_*ava 11

[[dictionary allKeys] count]; 会做的.