简体   繁体   中英

Filter NSDictionary from NSArray

I have One NSArray That contains NSDictionary Object with keys{"name","age","weight"} where the key name is NSString ,age is NSInteger ,weight is float Value

I need to filter the NSArray with the following conditions 1.name contains 'abc' 2.age below 18 3.weight less than 50

Answer will be Appreciated

This will work but is not elegant at all.

NSMutableArray *filteredArray = [[NSMutableArray alloc]init];
for (NSDictionary *dic in myArray){

int age = [[dic objectForKey:@"age"]intValue];

     if (age < 18){

        float weight = [[dic objectForKey:@"weight"]floatValue];
        if (float < 50){

        string name = [dic objectForKey:@"name"];
        if (![string rangeOfString:@"bla"].location == NSNotFound) {
            [filteredArray addObject:dic];
         }

        }
     }

}

The other elegant solution is to use the enumeration block and predicates to check your dictionary contents, but I'm not comfortable enough in objective-C to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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