如何从 NSMutableDictionary 获取给定对象的键?
我有一个对象位于一个大的 NSMutableDictionary 中,需要找出它有哪个键。所以我想从两列中查找该“表”。不仅可以使用键,还可以使用对象(获取键)。这可能吗?
I have an object which is in an big NSMutableDictionary, and need to find out which key this has. So I want to look up that "table" from both columns. Not only with keys, but also with objects (to get keys). Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看父类(NSDictionary)
,它将返回给定对象值的所有键的 NSArray。 但是它通过向字典的每个对象发送 isEqual 消息来实现这一点,因此对于大型数据集来说,这可能不是最佳性能。
也许您需要保存某种形式的附加索引结构,以允许您在其中的某些关键值上定位对象,链接到键而无需直接进行对象比较
Look to the parent class (NSDictionary)
Which will return a NSArray of all the keys for a given Object Value. BUT it does this by sending an isEqual message to each object of the Dictionary so for your large dataset this may not be best performance.
Maybe you need to hold some form of additional indexing structure structure(s) to allow you to locate the objects on some critical values within them, linked to the key without direct object comparison
要以更具体的方式回答您的问题,请使用以下命令获取特定对象的密钥:
To answer you question in a more specific manner, use the following to get a key for a particular object:
看看:
Take a look at:
使用 NSDictionary 的 block 方法绝对可以实现这一点。
您需要返回满足某些条件(谓词)的对象。
像这样使用它:
查看此答案以获取更多详细信息
如何指定NSDictionary的keysOfEntriesPassingTest所需的块对象/谓词?
That is definitely possible with NSDictionary's block method
You need to return objects which satisfy some condition (predicate).
Use it like this:
Check out this answer for more details
How do I specify the block object / predicate required by NSDictionary's keysOfEntriesPassingTest?