lu yuan lu yuan - 2 months ago 12
iOS Question

Filter Array with dictionaries using NSPredicate

There is an Array with each element being a

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];


How to filer the
arr
with name or email contains string
@"filter string"
by using
filterArrayUsingPredicate:
method.

Thanks in advance!

Nit Nit
Answer

Please see the below example..it may help you...

 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"]];

I hope it will help you...enjoy

Comments