0

我正在使用开放天气 API 来获取实时天气数据并将其显示在 UIViewController 中。但是我在 AppDelegate 中发出 http 请求。因此,我在 AppDelegate 中使用名为 weatherForcast() 的方法发出 API 请求,将 JSON 响应转换为 NSDictionary 对象,并将对象打印到控制台以确保一切正常,并且确实如此。

NSString *urllink = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%@&units=metric", lat, lng, WEATHERAPIKEY];
NSURL *jsonURL = [NSURL URLWithString:[self urlEncodeValue:urllink]];
NSString *jsonDataString  = [[NSString alloc]initWithContentsOfURL:jsonURL];
NSData *jsonData = [jsonDataString dataUsingEncoding:NSUTF16StringEncoding];
NSLog(@"This is jsonURL:%@", jsonURL);
NSError *err = nil;

if(jsonData == nil)
{
    NSLog(@"Error laoding jsonData");
}
else
{
    self.weatherInfo = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &err];
     NSLog(@"This is weatherInfo dictionary:%@", self.weatherInfo);
}

字典很完美。

然后在 viewDidLoad 的 UIViewController 中调用方法 weatherForecast(),然后调用方法 UpdateTemperature() 将标签的所有文本设置为字典中的数据。这是方法 UpdateTemperature 中的代码:

 NSLog(@"This is the  weatherInfo dictionary: %@", appDel.weatherInfo);

if([appDel.weatherInfo count] > 0 && appDel.isNetworkAvailable)
{
    NSLog(@"Went into weatherInfo.count > 0");
    lblCondition.text = [NSString stringWithFormat:@"condition:%@", [[[appDel.weatherInfo valueForKey:@"weather"] objectAtIndex:0] valueForKey:@"description"]];
    lblHumidity.text = [NSString stringWithFormat:@"humidity:%@", [[appDel.weatherInfo valueForKey:@"main"] valueForKey:@"humidity"]];
    lblTemperature.text = [NSString stringWithFormat:@"%@ Celsius", [[appDel.weatherInfo valueForKey:@"main"] valueForKey:@"temp"]];
    imgWeather.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", WEATHERCONDITIONIMGURL, [appDel.weatherInfo valueForKey:@"icon"]]]]];
    lblDegree.hidden = FALSE;
    [getTemp stopAnimating];

}
else
{
    lblDegree.hidden = TRUE;
}

只有当字典中至少有一个对象时,才会设置所有标签,这是应该的。但事实并非如此。所以我打印了字典,结果为零。

在 AppDelegate 中,当我打印字典时它很好,但在 viewDidLoad 中,当我打印同一个字典时,结果是 nil。怎么了?

4

2 回答 2

0

当您创建 appdelegate 的对象时,appdelegate 的所有变量都会重新初始化,因此它返回 nil。只需将您的代码放入一个函数中并简单地返回一个字典

请试试这个,

-(NSDictionary *) getWeatherInfo
{

    NSString *urllink = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%@&units=metric", 10.0, 10.0, @"api"];
    NSURL *jsonURL = [NSURL URLWithString:[self urlEncodeValue:urllink]];

    NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
    NSLog(@"This is jsonURL:%@", jsonURL);
    NSError *err = nil;

    NSDictionary *weather_info=[NSDictionary dictionary];
    if(jsonData == nil)
    {
        NSLog(@"Error laoding jsonData");

    }
    else
    {
       weather_info = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &err];
        NSLog(@"This is weatherInfo dictionary:%@", weather_info);


    }
    return weather_info;
}
于 2016-04-29T11:25:32.663 回答
0

It's likely that when viewDidLoad gets called, weatherInfo has not been initialized yet. If it requires an http call the data may have not returned yet and therefore when you access it in viewDidLoad there is no object to access. You might want to try reconfiguring where you make your http request and create weatherInfo.

于 2016-04-07T21:41:09.797 回答