简体   繁体   English

如何从数组中的字典的“值”中获取“键”?

[英]How to get 'key' from 'value' in dictionary which is inside an array?

Here is dictionary which in inside an array, 这是在数组内部的字典,

array With Dict : (
        {
        id = 1;
        name = Arjun;
    },
        {
        id = 2;
        name = Karan;
    },
        {
        id = 3;
        name = Ajit;
    },
        {
        id = 4;
        name = Prashant;
    },
        {
        id = 5;
        name = Sushant;
    }
)

When I will select any 'value', I want to fetch the 'key' associated with that value. 当我选择任何“值”时,我想获取与该值关联的“键”。

for example : 例如

Suppose I selected 'Prashant' and I want its 'id' ie 4. 假设我选择了“ Prashant”,并且想要它的“ id”,即4。

How to get 'key' from 'value'? 如何从“价值”中获取“钥匙”?

NSString *myName = @"Prashant";
for (NSDictionary *dict in array) {
    if ([dict[@"name"] isEqualToString:myName]) {
        NSLog(@"%@", dict[@"id"]);
        break;
    }
}

You can loop thru the Array and find the matching entry - value you are looking for. 您可以遍历数组并找到匹配的条目-您要查找的值。 Then get its key. 然后获取其密钥。

for (NSDictionary*dic in array)
{  
   NSString * name  = dic[@"name"];

   if([name isEqual:currentSelectedName])
   {
       NSString * id = dic[@"id"];

       NSLog(@"id is : %@",id);

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM