1

I've a .json file stored locally from which I'm loading in an array called countries as countries = (NSArray *)[NSJSONSerialization JSONObjectWithData [fileContents dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL]; after loading the filepath ofcourse. I'm Loading the .json into countries array as:

 NSString * filePath =[[NSBundle mainBundle] pathForResource:@"countries" ofType:@"json"];
    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


    if(error||!filePath)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }

    countries = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL];



After that I've taken an NSMutableArraycalled displayItems and initialized it as displayItems = [[NSMutableArray alloc] initWithArray:countries];.

Than in UITableView delegate methods I've used displayItems in count which after NSLog gives 248 means it's correctly getting the values cellForRowAtIndexPath and didSelectRowAtIndexPath

See this image below for the hierarchy of the ViewControllerHere is the hierarchy of my View Controller with a tableview and searchbar
THE PROBLEM: Based on the break points, this method is creating issue.

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchText length] == 0) {
        [displayItems removeAllObjects];
        [displayItems addObjectsFromArray:countries];
    } else {
        [displayItems removeAllObjects];
        for (NSString *string  in countries) {
            NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (r.location != NSNotFound) {
                [displayItems addObject:string];
            }
        }
    }
    [tableView reloadData];
}

When it reaches the line Range r the app crashes and the error is unrecognized selector sent to instance.
Everything is working great until I type a letter in the UISearchBar the app crashes at that time.

The Exact Error is:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x12ddd5e90'
*** First throw call stack:
(0x180a55900 0x1800c3f80 0x180a5c61c 0x180a595b8 0x18095d68c 0x1000d0e3c 0x185953d28 0x18577fe50 0x18577fdcc 0x185767a88 0x185953ad4 0x18578a8f8 0x18591fee8 0x18591f104 0x185aafae4 0x186095b20 0x18591ee70 0x185b5cc90 0x185b5c854 0x185772e84 0x181417e20 0x180a0cefc 0x180a0c990 0x180a0a690 0x180939680 0x181e48088 0x1857b0d90 0x1000e0e94 0x1804da8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • 1
    please post the exact error message, *what selector* to *what instance*!? – luk2302 Jan 10 '16 at 21:50
  • Most likely `countries` doesn't actually contain `NSString` objects. – rmaddy Jan 10 '16 at 22:06
  • 1
    Update your question with the log output of `countries` (at least enough to show its general structure). – rmaddy Jan 10 '16 at 22:07
  • @luk2302 I've updated the question based on your suggestion. – Chaudhry Talha Jan 10 '16 at 23:44
  • @rmaddy I've added how I'm assessing the `.json` file. But I don't think so that the problem is in the loading of file as the data is successful loading in the table and being selected. This has something to do with the delagate of searchbar. May be that's what I think. – Chaudhry Talha Jan 10 '16 at 23:46
  • 1
    The error message is clear. `countries` contains `NSDictionary` objects, not `NSString` objects. Your `for` loop is making a bad assumption. – rmaddy Jan 10 '16 at 23:48
  • @rmaddy Thank You so much! It's solved now. :D – Chaudhry Talha Jan 10 '16 at 23:58
  • 1
    Like I said earlier, update your question showing the contents of `countries`. No one can give you the proper answer without know its contents and value(s) you actually need from it. – rmaddy Jan 11 '16 at 00:01

0 Answers0