Reputation: 290
I have added SearchBar functionality to my tableview.(Note I dragged the SearchBar to the View and not onto the TableView). I have the following sub-process that throws an Exception error to crash my program when it gets to NSRange function. Below is my SearchBar
method. Can you see anything that might be causing the error? (I have included excerpt form Debug screen below)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if (searchText.length==0){
boolIsFiltered = NO;
}
else
{
boolIsFiltered = YES;
arrayFilteredproducts = [[NSMutableArray alloc]init];
for (NSString *str in arrayProducts) {
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location!=NSNotFound){
[arrayFilteredproducts addObject:str];
}
}
}
}
And here is the method that creates arrayProducts
.
- (void)request:(SFRestRequest *)request didLoadResponse:(id)jsonResponse {
NSMutableArray *records = [jsonResponse objectForKey:@"records"];
arrayProducts = records;
[self.tableProducts reloadData];
}
Here is the dump from Debug screen:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x176c5a80 First throw call stack:
(0x30076f8b 0x3a5166af 0x3007a927 0x3007920b'
Upvotes: 0
Views: 91
Reputation: 11
You should check that the JSON response is actually an array of strings for the object for key "records". You can put a breakpoint in your code and type in the console, "po jsonResponse", to see what the json response looks like. I think you are expecting the response to look something like...
{ "records" : [ "aString" , "anotherString", "so on" ] }
But I'm afraid the json data might actually look more like...
{ "records" : [ {...}, {...}, {...} ] }
So instead of the object for key "records" being an array of strings like you assume it is, it is more likely an array of dictionaries. This is evident from the runtime error you are receiving.
You can add more logic to your code to compensate, and as neilco already pointed out, a good method is to check that str is actually an instance of NSString before calling rangeOfString:options:
if ( [str isKindOfClass:[NSString class]] )
{
// code in here
}
Upvotes: 1
Reputation: 8012
The answer is in the exception message:
'-[__NSDictionaryM rangeOfString:options:]: unrecognized selector
This is saying that str
is a NSDictionary
. You should check that str
is actually an instance of NSString
before calling rangeOfString:options:
Upvotes: 0