Search code examples
iosobjective-cnsmutablearrayuisearchbarnsmutabledictionary

'NSInvalidArgumentException '-[NSTaggedPointerString count]: unrecognized selector sent to instance 0xa006162696a614e6'


I am a newbie and designing a weather forecast app as my first project in iOS.

I have designed a UISearchBar for searching the cities name. The search bar is working as:

1) Step 1: Type "Naji". It is showing two cities in table view: Naji Naji

2) Step 2: Type "Najib". It is not showing anything as the result is null.

(Error Part)

3) Step 3: Whenever I am typing "Najib", the app is terminating with the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString count]: unrecognized selector sent to instance 0xa006162696a614e6'


The code is:

    -(void)searchBar:(UISearchBar *)asearchBar textDidChange:(NSString *)searchText{

    if ([searchText length] <=                return;
    if ([searchText length]==0) {
            [discityname removeAllObjects];
            [discityname addObjectsFromArray:cityname];
    }else{
            [discityname removeAllObjects];
            NSURLSession *session=[NSURLSession sharedSession];
            NSString *complete_url=[NSString stringWithFormat:@"https://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20geo.places%%20where%%20text%%3D%%22%@%%25%%22&format=json&diagnostics=true&callback=",searchText];
            NSURLSessionDataTask *dataTask=[session dataTaskWithURL:[NSURL URLWithString:complete_url]completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
            NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"%@",json);
            NSMutableDictionary *currentDict = [[NSMutableDictionary alloc]initWithDictionary:json[@"query"]];
            int a=[[currentDict valueForKey:@"count"] intValue];
            if (a!=0) {
                cityname=[[NSMutableArray alloc]init];
           // if ([currentDict valueForKey: @"results"] != [NSNull null])
          //  {
                NSMutableDictionary *conDict = [[NSMutableDictionary alloc]initWithDictionary:currentDict[@"results"]];
                NSMutableArray *places=[conDict valueForKey:@"place"];
                NSMutableArray *country=[places valueForKey:@"country"];
                NSMutableArray *countryname=[country valueForKey:@"content"];
                NSMutableArray *placeName= [places  valueForKey:@"name"];
                NSMutableArray *centroid=[places valueForKey:@"centroid"];
                NSMutableArray *latitude=[centroid valueForKey:@"latitude"];
                NSMutableArray *longitude=[centroid valueForKey:@"longitude"];
                //if ([placeName isKindOfClass:[NSMutableArray class]]) {
                //}
                //cityname=[[NSMutableArray alloc]initWithArray:placeName];
                NSLog(@"%lu",placeName.count);
                for (int i=0; i<placeName.count; i++) {
                    [cityname addObject:[placeName objectAtIndex:i]];
                     [countrynames addObject:[countryname objectAtIndex:i]];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    discityname=[[NSMutableArray alloc]initWithArray:cityname];
                    discountryname=[[NSMutableArray alloc]initWithArray:countrynames];
                    latitudenames=[[NSMutableArray alloc]initWithArray:latitude];
                    longitudenames=[[NSMutableArray alloc]initWithArray:longitude];
                    [tableView reloadData];
                });
            }
           // for(int i=0;i<[placeName count];i++)
          //i  NSLog(@"The place is: %@ , %@, %@, %@",placeName[i],countryname[i],latitude[i],longitude[i]);
        }];
        [dataTask resume];
        for (NSString *string in discityname) {
            NSRange r=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (r.location!=NSNotFound) {
                [discityname addObject:string];
            }
        }
    }
    [tableView reloadData];
}

Answer: It should show city name "Najib" as returned in json.


Solution

  • You can add exception breakpoint in Xcode, it will tell you which line is crashing In Xcode, open the left panel, go to Show the breakpoint navigator, in the bottom left corner, click on the plus icon and add Exception breakpoint.

    exception breakpoint in Xcode