Mallikarjun Hanagandi
Mallikarjun Hanagandi

Reputation: 510

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' i.e 4.

How to get 'key' from 'value'?

Upvotes: 0

Views: 61

Answers (3)

Shehata Gamal
Shehata Gamal

Reputation: 100549

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

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

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

  }
}

Upvotes: 0

sergiog90
sergiog90

Reputation: 104

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

Upvotes: 2

Ari Singh
Ari Singh

Reputation: 1306

You can loop thru the Array and find the matching entry - value you are looking for. Then get its key.

Upvotes: 0

Related Questions