NSDictionary countForKey
那么是否有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管
[dictionary 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:would return the same as:
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):
字典表示一组 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.