I am having an array of dictionaries with keys "name", "image", "email" and "phone" as keys. I want to filter the dictionaries containing email and phones separately using nspredicate, passing the dictionary key as search string. How can I achieve this.
Asked
Active
Viewed 1,750 times
-4
-
1this is requirements .. where is your attempt??? – Bhavin Bhadani Mar 07 '16 at 10:45
-
your question is not clear, you want to predicate multiple key or single key – Anbu.Karthik Mar 07 '16 at 10:49
-
This is how I am doing, – Naveen Mar 07 '16 at 10:50
1 Answers
1
Use NSPredicate class to filter your array of dictionary by specified key,
For filter by email:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email contains[c] %@",strSearchString];
filteredArray = [[NSMutableArray alloc] initWithArray: [[arrayData filteredArrayUsingPredicate:predicate] copy]];
For filter by phone:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phone contains[c] %@",strSearchString];
filteredArray = [[NSMutableArray alloc] initWithArray: [[arrayData filteredArrayUsingPredicate:predicate] copy]];

Bharat Modi
- 4,158
- 2
- 16
- 27
-
-
2
-
and if you don't want it to be sensitive, just don't add [c] right? by definition its not case sensitive? – Ricardo Alves Mar 07 '16 at 10:54
-
Columns in NSPredicateFormat are indexed to improve performance of using IN operators [c] case insensitive: lowercase & uppercase values are treated the same [d] diacritic insensitive: special characters treated as the base character – Bharat Modi Mar 07 '16 at 11:01