4

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.

Himanth
  • 2,381
  • 3
  • 28
  • 41
XYZ
  • 73
  • 1
  • 7
  • Somewhere there is an object that you think is a `NSArray` but is in fact a `NSString` object. It could be the use of `valueForKey:`. – Larme Jul 18 '17 at 08:53
  • @XYZ, as you created multiple Mutable Arrays they will return you a nil object (As every Mutable Object need initialisation), replace your Mutable Arrays into NSArray first – Parvendra Singh Jul 18 '17 at 09:03
  • @ParvendraSingh Problem not solved yet! – XYZ Jul 18 '17 at 09:34
  • @Larme Can you please elaborate the solution – XYZ Jul 18 '17 at 09:37
  • Use the debugger and find exactly which line is causing the issue. You may also want to show us `json` in case it works, and in case it doesn't. – Larme Jul 18 '17 at 09:39
  • @Larme The link for json is: https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D'najiba'&format=json&diagnostics=true&callback= – XYZ Jul 18 '17 at 09:47
  • Just change the 'najiba' word with step 1, step 2, step 3 respectively – XYZ Jul 18 '17 at 09:48
  • Click breakpoint navigator in Xcode (cmd+7), Click + button on Bottom left corner -> Exception Breakpoint and Run again. Find exactly which line causes crash – Lal Krishna Jul 18 '17 at 09:58

1 Answers1

3

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

Floris M
  • 1,764
  • 19
  • 18
  • [placeName addObject:[[places objectAtIndex:j]valueForKey:@"name"]]; it is showing error is this line but unable to identify the problem – XYZ Jul 18 '17 at 10:12
  • Try to split your code in multiple line. Put [places objectAtIndex:j] in a variable "place". Same for [place valueForKey:@"name"] – Floris M Jul 18 '17 at 10:21
  • Yes, That was an issue with JSON data. Whenever the json is returning two values, the json's ("places") was in nsmutable array format but it was in nsdictionary in case of one value. Anyways Thanks for your suggestions. – XYZ Jul 19 '17 at 14:36
  • Ok, I'm always happy to help. If it helped you, can you please mark my response as an answer with the green check? – Floris M Jul 19 '17 at 14:40
  • Thanks for this answer... Got one of my issue solved due to this...Thank u ... :) – Prajnaranjan Das Feb 07 '19 at 19:45