如何在 iPhone 中获取 NSMutableDictionary 计数?

发布于 2024-12-26 06:01:01 字数 238 浏览 0 评论 0原文

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

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

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

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]);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2025-01-02 06:01:01

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

NSArray * allKeys = [mutableDictionary allKeys];
NSLog(@"Count : %d", [allKeys count]);

编辑

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

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

• 通过修复 mutableDictionary 不知何故为 nil 的错误,

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

  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

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 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.

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
能否归途做我良人 2025-01-02 06:01:01

[[dictionary allKeys] count]; 就可以了。

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

煮茶煮酒煮时光 2025-01-02 06:01:01
     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Rakesh",@"Name",@"[email protected]",@"Email",nil];
     NSLog(@"Count : %d", [dict count])

试试这个

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

Try this

冷清清 2025-01-02 06:01:01

如果您希望计算可以使用的任何数组项的 NSMutableDictionary 值。

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

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

NSLog(@"myDictionaryCount %lu", (unsigned long)[myDictionary[@"items"] count]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文