0

Here is my code :

- (void)searchTextDidChange:(TaggingSearchBar *)searchBar
                       text:(NSString *)searchText {


    NSArray *allPlayers = self.sessionModel.taggingModel.PlayersFromDatabase;
    [self.filterdPlayers removeAllObjects];
    if (searchText.length > 0) {
        self.isFilterd = YES;
        for (Player *p in allPlayers) {
            NSString *playerDescription = [NSString stringWithFormat:@"%d %@", p.jerseyNumber.intValue, p.fullName];
            if ([playerDescription rangeOfString:searchText].location != NSNotFound) {
                [self.filterdPlayers addObject:playerDescription];
            }
        }
    }
    else {
        self.filterdPlayers = [allPlayers mutableCopy];
        self.isFilterd = NO ;
    }
}

Something is wrong because when I'm debugging and I put C (I have a person with the name Cat ) i get nil in self.filtredPlayers. Debugger showed hat stood in this place , and if all ok but nothing happened. What could cause this ? I tried containsstring + addObject:p

hds
  • 33
  • 1
  • 8
  • Are you sure that at some point `playerDescription` did contain the string `Cat` in it? – Aviram Feb 22 '16 at 09:15
  • Yes I'm sure. My string is C or CA and playerDescription contains 0 Tommy Cat. (0 is a jersey number) – hds Feb 22 '16 at 09:19

2 Answers2

2

The code looks fine in terms of functionality. To be on the safe side, I would change the inner if clause to this:

if (searchText && [playerDescription rangeOfString:searchText].length) {

This is safer, because if searchText is nil, your code would crash. I understand that you verify it before, but it's the best practice.

Moreover, I think your problem is due to the fact that you add the string object, instead of the Player object. When the search term is empty/nil, you add the Player objects:

self.filterdPlayers = [allPlayers mutableCopy];

But when the search term exists, you add the string playerDescription instead of the Player object:

[self.filterdPlayers addObject:p];
Aviram
  • 3,017
  • 4
  • 29
  • 43
  • I tried add Player object aswell but it didn't work too. – hds Feb 22 '16 at 09:25
  • When you debugged your code, did it ever reached this line: `[self.filterdPlayers addObject:playerDescription];`? – Aviram Feb 22 '16 at 09:30
  • Yes and nothing happen – hds Feb 22 '16 at 09:31
  • Perhaps someone touches `self.filterdPlayers` afterwards? Put a breakpoint on the instance variable and see if it changes after this function has finished. – Aviram Feb 22 '16 at 09:33
  • Also, keep in mind that your function is called every time the user types something in the search bar, so if they type C and then CA it will be invoked twice. – Aviram Feb 22 '16 at 09:40
0

Use This

NSString *search_str = [NSString stringWithFormat:@"%@%@",searchBar.text, searchText];

   if ( playerDescription.containsString(searchBar.text)) //instead of  if ([playerDescription rangeOfString:searchText].location != NSNotFound)
Reshmi Majumder
  • 961
  • 4
  • 15
  • Careful using `containsString`, if this apps runs on iOS 7 it will crash (this function was introduced on iOS 8). – Aviram Feb 22 '16 at 09:39
  • Use this NSRange nameRange = [playerDescription rangeOfString: search_str options:NSCaseInsensitiveSearch]; if(nameRange.location != NSNotFound ) – Reshmi Majumder Feb 22 '16 at 09:41
  • Ok i got it. Xcode didnt use my array alloc init in setup fucn. – hds Feb 22 '16 at 12:14