0

I am trying to use a location-based Weather app and it is crashing as soon as I open the view for that page.

This is the method;

- (WeatherModel *) parseWeatherData: (NSData *) data {
    
    WeatherModel *weatherModel = [[WeatherModel alloc] init];
    NSError *error;
    
    id jsonObject = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingAllowFragments error: &error];
    
    if (error) {
        NSLog(@"Error decoding JSON data : %@", error.localizedDescription);
        [self->_delegate didFailedWithError: error];
        return nil;
    }
    
    if ([jsonObject isKindOfClass: [NSDictionary class]]) {
        
        NSMutableDictionary *dicJsonObject = [[NSMutableDictionary alloc] initWithDictionary: jsonObject];
        NSString *cityName = [dicJsonObject valueForKey: @"name"];
        double temperature = [[[dicJsonObject valueForKey: @"main"] valueForKey: @"temp"] doubleValue];
        NSMutableArray *arrWeatherData = [[NSMutableArray alloc] initWithArray: [dicJsonObject valueForKey: @"weather"]];
        
        weatherModel.strCityName = cityName;
        weatherModel.temperature = temperature;
        weatherModel.weatherConditionID = [[[arrWeatherData objectAtIndex: 0] valueForKey: @"id"] intValue];
        
    }
    
    return weatherModel;
    
}

It is crashing on this line;

if (error) {
        NSLog(@"Error decoding JSON data : %@", error.localizedDescription);
        [self->_delegate didFailedWithError: error];
        return nil;
    }

If I comment out the NSLog section and [self->_delegate didFailedWithError: error]; the app does not crash, but then does not function as expected either...

The second part of the crash using debug is here;

- (void) fetchWeatherForCity: (NSString *) cityName {
    
    _strWeatherURL = [NSString stringWithFormat: @"https://api.openweathermap.org/data/2.5/weather?q=%@&appid=bfea07812845ff9cb7e", cityName];
    
    NSURL *weatherURL = [[NSURL alloc] initWithString: _strWeatherURL];
    
    NSURLSessionConfiguration *urlSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration: urlSessionConfiguration];
    
    NSURLSessionDataTask *task = [urlSession dataTaskWithURL: weatherURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            NSLog(@"Error fetching weather data : %@", error);
            [self->_delegate didFailedWithError: error];
            return;
        }
        
        WeatherModel *weatherModel = [[WeatherModel alloc] init];
        weatherModel = [self parseWeatherData: data];
        if (weatherModel != nil) {
            [self->_delegate didUpdateWeather: weatherModel];
        }
        
    }];
    [task resume];
    
}

On this line;

weatherModel = [self parseWeatherData: data];
user1695971
  • 103
  • 1
  • 5
  • 15
  • Can you share a crash log? Also you say "it is crashing on this line", but then you show several lines - please indicate what line it crashes. – koen Feb 22 '21 at 15:06
  • In the first section, anything inside the `if (error) {` causes it to crash (except return nil;) – user1695971 Feb 22 '21 at 15:08
  • Did you enable exception breakpoints in Xcode? link: https://stackoverflow.com/questions/17802662/how-to-add-exception-breakpoint-in-xcode – koen Feb 22 '21 at 15:10
  • It stops here; `NSLog(@"Error decoding JSON data : %@", error.localizedDescription);` The `error.localizedDescription` in particular is highlighted under red. – user1695971 Feb 22 '21 at 15:14
  • And what exactly is the error? Difficult to help if you don't share all relevant info. – koen Feb 22 '21 at 15:17
  • This is the problem, nothing is showing in the debug console regarding the error, – user1695971 Feb 22 '21 at 15:22
  • Could you show a screenshot of XCode when it "stops"? – Larme Feb 22 '21 at 18:12
  • Unreleated, but `WeatherModel *weatherModel = [[WeatherModel alloc] init]; weatherModel = [self parseWeatherData: data];` should just be `WeatherModel *weatherModel = [self parseWeatherData: data];` because you are already doing an init/alloc inside the method, you are doing one for "nothing". `NSMutableDictionary *dicJsonObjec` shouldn't be a `NSMutableDictionary`, a `NSDictionary` should be enough. There is no needs for `NSJSONReadingAllowFragments` since you expects a `NSDictionary`. – Larme Feb 22 '21 at 18:15
  • 1
    That should probably be ```didFailWithError``` and not ```didFailed...``` – skaak Feb 23 '21 at 15:46

0 Answers0