Filter Array with dictionaries using NSPredicate

前端 未结 2 1363
别那么骄傲
别那么骄傲 2020-12-09 15:56

There is an Array with each element being a NSDictionary.

NSMutableArray *mutArr = [NSMutableArray array];

for (Person *person in persons) {
           


        
相关标签:
2条回答
  • 2020-12-09 16:21

    Please see the below example:

     NSArray *array = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"filter string" forKey:@"email"]];   // you can also do same for Name key... 
        NSArray *filteredarray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(email == %@)", @"filter string"]];
    
    0 讨论(0)
  • 2020-12-09 16:40

    Check this out :

    var customerNameDict = ["firstName":"karthi","LastName":"alagu","MiddleName":"prabhu"];
    var clientNameDict = ["firstName":"Selva","LastName":"kumar","MiddleName":"m"];
    var employeeNameDict = ["firstName":"karthi","LastName":"prabhu","MiddleName":"kp"];
    
    var attributeValue = "karthi";
    
    var arrNames:Array = [customerNameDict,clientNameDict,employeeNameDict];
    
    var namePredicate = NSPredicate(format: "firstName like %@",attributeValue);
    
    let filteredArray = arrNames.filter { namePredicate.evaluateWithObject($0) };
    println("names = ,\(filteredArray)");
    
    0 讨论(0)
提交回复
热议问题