简体   繁体   English

从 NSArray 过滤 NSDictionary

[英]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我有一个包含 NSDictionary 对象的 NSArray,其键为 {"name","age","weight"},其中键名是 NSString ,age 是 NSInteger ,weight 是浮点值

I need to filter the NSArray with the following conditions 1.name contains 'abc' 2.age below 18 3.weight less than 50我需要使用以下条件过滤 NSArray 1.name contains 'abc' 2.age under 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.另一个优雅的解决方案是使用枚举块和谓词来检查您的字典内容,但我在 Objective-C 中不太习惯这样做。

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

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