NSDictionary countForKey

发布于 2024-10-16 17:31:48 字数 119 浏览 3 评论 0原文

那么是否有 countForKey 或类似于 NSDictionary 的东西?我确实知道有一个 count 方法,但是是否有类似 countForKey 的方法,或者我是否必须为其创建一个 NSDictionary 类别?

So is there a countForKey or something similar to that for NSDictionary? I do know that there is a count method but is there something like countForKey or do I have to make a NSDictionary category for it?

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

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

发布评论

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

评论(2

何以心动 2024-10-23 17:31:48

尽管 [dictionary count] 似乎是最简单的方法,但您可以从字典中获取键数组,然后检索数组计数,因此:

NSUInteger keyCount = [dictionary count];

将返回与以下内容相同的内容:

NSUInteger keyCount = [[dictionary allKeys] count];

编辑:< /强>
根据您在评论中提供的附加信息,如果您想找出有多少个“密钥”(尽管您的真实要求仍然不清楚):

NSUInteger keyCountForObject = [[dictionary allKeysForObject:object] count];

Even though [dictionary count] seems like the easiest approach, you could get the array of keys from the dictionary and then retrieve the array count, so:

NSUInteger keyCount = [dictionary count];

would return the same as:

NSUInteger keyCount = [[dictionary allKeys] count];

EDIT:
With the additional information you provided in the comment, if you want to find out how many 'keys' (although your true requirement still isn't that clear):

NSUInteger keyCountForObject = [[dictionary allKeysForObject:object] count];
神经暖 2024-10-23 17:31:48

字典表示一组 1:1 关系。每个键仅代表一个对象。因此,如果字典中没有表示键值对,则这个假设的 -countForKey: 将始终返回 0,如果有,则始终返回 1。字典中永远不可能有代表两个或多个对象的键。

Dictionaries represent a set of 1:1 relationships. Every key represents just one object. So this hypothetical -countForKey: would always return 0 if the key-value pair were not represented in the dictionary, and 1 if it were. You can never have a key in a dictionary representing two or more objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文