GuybrushThreepwood
GuybrushThreepwood

Reputation: 5616

Search Bar Controller - Crash When Searching for Results

I am implementing a search bar controller to search a table view. The below method code which performs the search is crashing with the error "-[__NSArrayM rangeOfString:options:]: unrecognized selector sent to instance 0x65558e0'

The locationInfo array is an array containing 26 arrays, each of which contains a number of objects made up of strings.

Can anyone suggest why the code is crashing ?

Thank you.

- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];

if ([self searchResults] == nil)
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setSearchResults:array];
    [array release], array = nil;
}

[[self searchResults] removeAllObjects];

if ([[self savedSearchTerm] length] != 0)
{
    for (NSString *currentString in [self locationInfo])
    {
        if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            [[self searchResults] addObject:currentString];
        }
    }
}
}

Upvotes: 1

Views: 429

Answers (2)

objectivecdeveloper
objectivecdeveloper

Reputation: 1050

As you stated in question that "locationInfo" is array containing 26 arrays, so, currentString in [self locationInfo] will return an array only so try to write something like below :

for (NSArray *currentArray in [self locationInfo])

{
for (NSString *currentString in currentArray)
{
    if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location !=  NSNotFound)
    {
        [[self searchResults] addObject:currentString];
    }
}

}

or something like this

Upvotes: 2

jv42
jv42

Reputation: 8593

Based on the error you're getting, it seems [self locationInfo] returns an array (NSArray) and not a string (NSString) as you're expecting.

Upvotes: 0

Related Questions