ios 如何在 iphone 中获取 NSMutableDictionary 计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8815375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:14:35  来源:igfitidea点击:

How to get NSMutableDictionary count in iphone?

iphoneioscountnsmutabledictionary

提问by Gopinath

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.

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

NSLog(@"Count : %d", [mutableDictionary count]);

It is always returns '0'. How to get the count of NSMutableDictionary in iPhone? Thanks in advance.

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

回答by Jesse Black

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

编辑

Upon looking through the dictionary documentation, NSDictionary's countmethod (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.

查看字典文档后,NSDictionarycount方法(或属性)也应该有效。我认为您可能已经收到 0 计数,因为字典是空的或为零。我提供了我的解决方案,因为我更关心枚举键而不是直接计算条目。

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

请考虑您已在其他地方解决了该问题。
? 通过实际填充字典

?通过修复 mutableDictionary 以某种方式为零的错误

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

回答by Ankit Srivastava

[[dictionary allKeys] count];will do the trick.

[[dictionary allKeys] count];会做的伎俩。

回答by Rakesh Bhatt

     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Rakesh",@"Name",@"[email protected]",@"Email",nil];
     NSLog(@"Count : %d", [dict count])

Try this

尝试这个

回答by Sid

if you looking to count NSMutableDictionary values for any Array item you can use.

如果您想为您可以使用的任何 Array 项目计算 NSMutableDictionary 值。

NSLog(@"myDictionaryCount %lu", (unsigned long)[myDictionary[@"items"] count]);