The Fuud People
The Fuud People

Reputation: 13

Find NSDictionary containing specific value in NSArray (iOS)

I have a NSArray("address_components") of multiple NSDictionary. Each NSDictionary contains one NSArray("types") of NSString values. Like this..

"address_components" =         (
{
    "long_name" = "Perumbavoor Puthencruz Road";
    "short_name" = "Perumbavoor Puthencruz Rd";
    types =                 (
        route
    );
},
{
    "long_name" = Vengola;
    "short_name" = Vengola;
    types =                 (
        locality,
        political
    );
},
{
    "long_name" = Ernakulam;
    "short_name" = EKM;
    types =                 (
        "administrative_area_level_2",
        political
    );
},
{
    "long_name" = Kerala;
    "short_name" = KL;
    types =                 (
        "administrative_area_level_1",
        political
    );
},
{
    "long_name" = India;
    "short_name" = IN;
    types =                 (
        country,
        political
    );
},
{
    "long_name" = 683556;
    "short_name" = 683556;
    types =                 (
        "postal_code"
    );
}
);

How can I get the dictionary that contains the array with string "locality". In this example I want to get the dictionary..

{
  "long_name" = Vengola;
  "short_name" = Vengola;
   types =(
      locality,
      political
   );
}

Thank you :)

Upvotes: 1

Views: 770

Answers (4)

jp2g
jp2g

Reputation: 692

Here ya go:

NSArray *list = @[
              @{
                  @"long_name" : @"Perumbavoor Puthencruz Road",
                  @"short_name" : @"Perumbavoor Puthencruz Rd",
                  @"types" : @[@"route"]
                  },
              @{
                    @"long_name" : @"Vengola",
                    @"short_name" : @"Vengola",
                    @"types" : @[@"locality", @"political"]
                    },
              @{
                  @"long_name" : @"Ernakulam",
                  @"short_name" : @"EKM",
                  @"types" :                 @[
                          @"administrative_area_level_2",
                          @"political"]
                  }
              ];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.types CONTAINS 'locality'"];
NSArray *result = [list filteredArrayUsingPredicate:predicate];
NSLog(@"result: %@", result);

Result:

{
        "long_name" = Vengola;
        "short_name" = Vengola;
        types =         (
            locality,
            political
        );
    }

Upvotes: 0

balkaran singh
balkaran singh

Reputation: 2786

plz use this code

NSArray *myArray = [yourAddressDict valueForKey:"address_components"];


 NSMutableArray *NewArray = [NSMutableArray array];

    for (NSDictionary *ob in myArray) {
        NSArray *arraylocality = [ob valueForKey:@"types"];
         BOOL isthere=  [arraylocality containsObject:@"locality"];
        if (isthere) {
            [NewArray addObject:ob];
        }

    }

 NSLog(@"%@",NewArray);

Upvotes: 2

Nirav D
Nirav D

Reputation: 72410

You need to predicate your address array like this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY types CONTAINS[c] %@", @"locality"];
NSArray *arr = [yourArray filteredArrayUsingPredicate:predicate];

Upvotes: 2

Saurabh Jain
Saurabh Jain

Reputation: 1698

You can check the array contain the dictionary or bot:

for(int i=0; i<[arr count];i++){
if([arr[i] isKindOfClass:[NSDictionary Class]]){
NSDictionary *dic=arr[i];
NSLog("%@",dic);
}
}

Upvotes: 0

Related Questions