如何从 NSMutableDictionary 获取给定对象的键?

发布于 2024-08-31 20:30:57 字数 93 浏览 7 评论 0原文

我有一个对象位于一个大的 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 技术交流群。

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

发布评论

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

评论(4

許願樹丅啲祈禱 2024-09-07 20:30:57

查看父类(NSDictionary)

- (NSArray *)allKeysForObject:(id)anObject

,它将返回给定对象值的所有键的 NSArray。 但是它通过向字典的每个对象发送 isEqual 消息来实现这一点,因此对于大型数据集来说,这可能不是最佳性能。

也许您需要保存某种形式的附加索引结构,以允许您在其中的某些关键值上定位对象,链接到键而无需直接进行对象比较

Look to the parent class (NSDictionary)

- (NSArray *)allKeysForObject:(id)anObject

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

—━☆沉默づ 2024-09-07 20:30:57

要以更具体的方式回答您的问题,请使用以下命令获取特定对象的密钥:

NSString *knownObject = @"the object";
NSArray *temp = [dict allKeysForObject:knownObject];
NSString *key = [temp objectAtIndex:0];

//"key" is now equal to the key of the object you were looking for

To answer you question in a more specific manner, use the following to get a key for a particular object:

NSString *knownObject = @"the object";
NSArray *temp = [dict allKeysForObject:knownObject];
NSString *key = [temp objectAtIndex:0];

//"key" is now equal to the key of the object you were looking for
淡墨 2024-09-07 20:30:57

看看:

- (NSArray *)allKeysForObject:(id)anObject

Take a look at:

- (NSArray *)allKeysForObject:(id)anObject
情深已缘浅 2024-09-07 20:30:57

使用 NSDictionary 的 block 方法绝对可以实现这一点。

- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate;

您需要返回满足某些条件(谓词)的对象。

像这样使用它:

    NSSet *keys = [myDictionary keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {

       BOOL found = (objectForWhichIWantTheKey == obj);
       if (found) *stop = YES;
       return found;

    }];

查看此答案以获取更多详细信息

如何指定NSDictionary的keysOfEntriesPassingTest所需的块对象/谓词?

That is definitely possible with NSDictionary's block method

- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate;

You need to return objects which satisfy some condition (predicate).

Use it like this:

    NSSet *keys = [myDictionary keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {

       BOOL found = (objectForWhichIWantTheKey == obj);
       if (found) *stop = YES;
       return found;

    }];

Check out this answer for more details

How do I specify the block object / predicate required by NSDictionary's keysOfEntriesPassingTest?

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