-2

World Weather Online から JSON を取得しています。ラベルに気温を華氏で表示しようとしていますが、実行すると、そのラベルは (null) と表示されます。JSON はコンソールに正常に表示されるので、正しいデータを取得していることがわかります。何か案は?

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define weatherURL [NSURL URLWithString: @"http://api.worldweatheronline.com/free/v1/weather.ashx?q=47129&format=json&num_of_days=5&date=today&key=37a5fj42xpyptvjgkhrx5rwu"]

#import "Weather.h"
#import "WeatherLocation.h"

@interface Weather ()

@end

@implementation Weather

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    dispatch_async(kBgQueue, ^{
        NSData *data = [NSData dataWithContentsOfURL:weatherURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData
{
    // parse out the JSON data
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSArray *weather = [json objectForKey:@"data"];

    NSLog(@"weather: %@", weather);

    NSArray *temp = [json objectForKey:@"temp_F"];

    NSDictionary *weatherConditions = [temp objectAtIndex:0];

    NSString *tempF = [weatherConditions objectForKey:@"temp_F"];

    currentTemp.text = [NSString stringWithFormat:@"%@", tempF];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

の結果NSLog(@"weather: %@", weather);

weather: {
    "current_condition" =     (
                {
            cloudcover = 50;
            humidity = 83;
            "observation_time" = "11:28 AM";
            precipMM = "0.0";
            pressure = 1016;
            "temp_C" = 22;
            "temp_F" = 72;
            visibility = 13;
            weatherCode = 116;
            weatherDesc =             (
                                {
                    value = "Partly Cloudy";
                }
            );
            weatherIconUrl =             (
                                {
                    value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
                }
            );
            winddir16Point = N;
            winddirDegree = 0;
            windspeedKmph = 0;
            windspeedMiles = 0;
        }
    );
    request =     (
                {
            query = 47129;
            type = Zipcode;
        }
    );
    weather =     (
                {
            date = "2013-09-09";
            precipMM = "0.0";
            tempMaxC = 35;
            tempMaxF = 95;
            tempMinC = 21;
            tempMinF = 69;
            weatherCode = 113;
            weatherDesc =             (
                                {
                    value = Sunny;
                }
            );
            weatherIconUrl =             (
                                {
                    value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png";
                }
            );
            winddir16Point = SSW;
            winddirDegree = 210;
            winddirection = SSW;
            windspeedKmph = 12;
            windspeedMiles = 7;
        }
    );
}

投稿したコードでこれを機能させようとしていました。天気配列を使用すると、アプリがクラッシュNSDictionary *weatherConditions = [weather objectAtIndex:0];します[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa158f70

更新: temp_F に対して NULL を返す問題を修正しました。

現在のコードは次のとおりです。

- (void)fetchedData:(NSData *)responseData
{
    // parse out the JSON data
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"JSON: %@", json);

    NSDictionary *data = [json objectForKey:@"data"];
    NSArray *currentConditions = [data objectForKey:@"current_condition"];
    NSDictionary *conditions = [currentConditions objectAtIndex:0];

    NSArray *icon = [data objectForKey:@"weatherIconUrl"];
    NSDictionary *weatherIcon = [icon objectAtIndex:0];

    NSString *tempF = [NSString stringWithFormat:@"%@\u00B0", [conditions objectForKey:@"temp_F"]];
    NSString *imageURL = [NSString stringWithFormat:@"%@", [weatherIcon objectForKey:@"value"]];
    NSURL *conditionIcon = [NSURL URLWithString:imageURL];
    NSData *imageData = [NSData dataWithContentsOfURL:conditionIcon];
    UIImage *conditionImage = [UIImage imageWithData:imageData];

    NSLog(@"Image URL: %@", imageURL);
    conditionsImage.image = conditionImage;

    NSLog(@"temp_F: %@", tempF);

    currentTemp.text = [NSString stringWithFormat:@"%@", tempF];
    [currentTemp setFont:[UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:72.0]];
}

私が今抱えている唯一の問題は、weatherIconUrl が現在 NULL を返していることです。その理由はわかりません。

4

1 に答える 1