I am trying to search for names using NSPredicate in my Code. The search works but it does not return the appropriate results. When i search for a name e.g "Colin" it returns all the other names in the table or another name like "Mike" but if i enter a random string that does not exist, it returns :"No Result Found". When i type a name in the search bar (e.g Lisa) , i expect it to find the name (Lisa) and return it but it doesn't do that
This is my code:
- (void)filterData {
[self.filteredSearch removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS [cd] %@", mySearchBar.text];
self.filteredSearch = [[self.name filteredArrayUsingPredicate:predicate] mutableCopy];
}
self.name returns all the names in the table and it gets this from a property list
NSString *path = [[NSBundle mainBundle] pathForResource:@"lecturers" ofType:@"plist"];
lecturers= [[NSDictionary alloc] initWithContentsOfFile:path];
// Allocate key objects from lecturers list
name = [lecturers objectForKey:@"name"];
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self filterData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.tableView) {
return self.name.count;
}
[self filteredSearch];
return self.filteredSearch.count;
}
How can I fix this? I have tried using self MATCHES and other NSPredicate formats but they don't seem to work.
self.nameis, what result you expect, what result you get. - mattself.namecontains and what "wrong answers" you are getting. However I do have one suggestion; usepredicateWithBlock:instead ofpredicateWithFormat:; it is a lot easier and more powerful. - matt