You can get the keys matching the value with allKeysForObject
. Your dictionary could have more than one key for the same object.
Retrieve key for a given value in an NSDictionary
-
06-10-2022 - |
Pergunta
I'm trying to avoid creating multiple dictionaries, one where the key is the value, and another where the value is the key.
So I can easily look for a value if I have the key by doing
[self.myDic objectForKey: word];
But is there a way to go through the values and see if it matches word
, and if it does, then give me the key? Or do I have to iterate through the whole thing?
Solução
Outras dicas
you can get all the keys with yourDict.allKeys
and iterate them until you find the right object .
for (NSString* key in yourDict.allKeys) {
if([yourDict objectForKey:key] isEqualToString:@"somethinghere object/other equalitu"){
return key;
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow