【问题标题】:How to get 'key' from 'value' in dictionary which is inside an array?如何从数组内的字典中的“值”中获取“键”?
【发布时间】:2018-02-10 11:21:21
【问题描述】:

这是一个数组里面的字典,

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

当我选择任何“值”时,我想获取与该值关联的“键”。

例如

假设我选择了“Prashant”并且我想要它的“id”,即 4。

如何从'value'中得到'key'?

【问题讨论】:

  • 尝试回答.......

标签: ios objective-c arrays key nsdictionary


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

【讨论】:

  • 这也适用于我。谢谢,@sergiog。但我已经接受了之前的答案。
【解决方案2】:
for (NSDictionary*dic in array)
{  
   NSString * name  = dic[@"name"];

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

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

  }
}

【讨论】:

    【解决方案3】:

    您可以遍历数组并找到匹配的条目 - 您正在寻找的值。然后得到它的钥匙。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2021-05-25
      相关资源
      最近更新 更多