如何在 iphone 中获取 NSMutableDictionary 计数? [英] How to get NSMutableDictionary count in iphone?

查看:30
本文介绍了如何在 iphone 中获取 NSMutableDictionary 计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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]);

编辑

查看字典文档后,NSDictionarycount 方法(或属性)应该也能工作.我认为您可能已经收到 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆