Reputation: 109
Currently trying to setup a search would search an Array (bakeryProductArray) which looks like this
"Tea Loaf", Tiramusu, "Treacle Loaf", Trifle, "Triple Chocolate Brownie", Truffles, "Various sponges", "Viennese Whirls", "Wedding Cakes"
with what the users types into the UISearchbar. I am unclear on how to represent each item in the array in the CFString.
The code I currently have is.
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
searchSearch = [NSPredicate predicateWithFormat:@"self CONTAINS[cd]",searchText];
//@"%K CONTAINS[cd] %@"
searchResults = [bakeryProductArray filteredArrayUsingPredicate:searchSearch];
NSLog(@"Filtered Food Product Count --> %d",[searchResults count]);
}
Can answer any questions and supply more code if needed.
Upvotes: 2
Views: 4780
Reputation: 539755
Is this what your are looking for?
NSArray *bakeryProductArray = @[@"Tea Loaf", @"Tiramusu", @"Treacle Loaf", @"Trifle"];
NSString *searchText = @"tr";
NSPredicate *searchSearch = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText];
NSArray *searchResults = [bakeryProductArray filteredArrayUsingPredicate:searchSearch];
NSLog(@"%@", searchResults);
// "Treacle Loaf",
// Trifle
This finds all strings that contain the given search string (case insensitive). Alternatively, you can use "=" or "BEGINSWITH", depending on your needs. (More information about predicates in the "Predicate Programming Guide" .)
Upvotes: 6