Question

I'm getting started with Objective-C, and can't seem to extract the data being pulled by AFNetworking to use as label text, etc. I'm using it for a weather app backed by Forecast.io, and the Forecastr API wrapper. If the API is writing the JSON to a dictionary, I can't seem to find it.

In the main view controller:

[forecastr getForecastForLocation:location time:nil exclusions:nil success:^(id JSON) {
        NSLog(@"JSON response was: %@", JSON);
        NSLog(@"Testing: %@", [JSON valueForKey:kFCSummary]);

    } failure:^(NSError *error, id response) {
        NSLog(@"Error while retrieving forecast: %@", [forecastr messageForError:error withResponse:response]);
    }];
}

The first NSLog line will return the full JSON object, serialized and everything, but the second one just puts out NULL.

In the Forecastr.m file:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
if (self.callback) {
    AFHTTPRequestOperation *httpOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *JSONP = [[NSString alloc] initWithData:responseObject encoding:NSASCIIStringEncoding];
        if (self.cacheEnabled) [self cacheForecast:JSONP withURLString:cacheKey];
        success(JSONP);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error, operation);
    }];
    [pendingRequests addObject:httpOperation];
    [forecastrQueue addOperation:httpOperation];
} else {
    AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if (self.cacheEnabled) [self cacheForecast:JSON withURLString:cacheKey];
        success(JSON);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        failure(error, JSON);
    }];
    [pendingRequests addObject:jsonOperation];
    [forecastrQueue addOperation:jsonOperation];
}

I can tell that this reading for a cached forecast, and then pulling down a new JSON file if the cache is too old. But I can't tell where it's putting the JSON, and it's not in a dictionary which is how most tutorials and resources use.

I've tried a bunch of different things already, such as creating my own dictionary, but it won't add the JSON object in there. I've also tried to use the returned JSON object as a dictionary and a key-value pair, but nothing seems to be working. I've been banging my head against the wall for days, but there's hardly anyone out there using Forecastr with a problem.

Was it helpful?

Solution

The problem is that the data you are getting back is split into a number of different sections (such as the current day and each of the days of the week). You can't just ask the overall data set for the temperature, you need to navigate to the 'day' that you're interested in and then request the temperature. Such as:

[[JSON objectForKey:kFCCurrentlyForecast] objectForKey:kFCTemperature]

OTHER TIPS

JSON seems to be a string (github). In order to parse this, you can use a library such as JSONKit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top