In case if you want to show results from array
. You can use NSPredicate
. You need to add all strings in array
and take another array for showing filtered results like:
//filter content from array:
- (void)filterContentForSearchText:(NSString*)searchText {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
YOUR_FILTERED_ARRAY = [[YOUR_ARRAY filteredArrayUsingPredicate:resultPredicate] mutableCopy];
}
In case you have array of dictionaries then just replace the filtration line with this one
YOUR_FILTERED_ARRAY = [[YOUR_ARRAY filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(name contains[c] %@)", searchText]]mutableCopy];
//name is the "key" and search text is the matching value!
REFERENCE for predicate.
Apple API Doc for filteredarrayusingpredicate
.
SWIFT 3x:
//filter content from array:
func filterContent(forSearchText searchText: String) {
let resultPredicate = NSPredicate(format: "SELF contains[c] %@", searchText)
YOUR_FILTERED_ARRAY = YOUR_ARRAY.filter { resultPredicate.evaluate(with: $0) }
}
YOUR_FILTERED_ARRAY = YOUR_ARRAY.filter { NSPredicate(format: "(name contains[c] %@)", searchText).evaluate(with: $0) }