24

有一个数组,每个元素都是一个NSDictionary.

NSMutableArray *mutArr = [NSMutableArray array];

for (Person *person in persons) {
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:person.name, @"name", person.email, @"email", nil];
    [mutArr addObject:dict];
}

self.arr = [[NSArray alloc] initWithArray:mutArr];

如何通过 using方法归档arr名称或电子邮件包含字符串。@"filter string"filterArrayUsingPredicate:

提前致谢!

4

2 回答 2

50

请看下面的例子:

 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"]];
于 2012-08-23T03:36:24.853 回答
1

看一下这个 :

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)");
于 2016-09-23T12:03:06.190 回答