在AppDelegate中初始化的字典在UIViewController中具有nil值

时间:2016-04-07 21:34:03

标签: ios objective-c nsdictionary appdelegate openweathermap

我正在使用开放天气API来获取实时天气数据并将其显示在UIViewController中。但是我在AppDelegate中发出了http请求。所以我在一个名为weatherForcast()的方法中在AppDelegate中创建了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中,它结果是零。怎么了?

2 个答案:

答案 0 :(得分:0)

当调用viewDidLoad时,天气信息尚未初始化。如果它需要http调用,则数据可能尚未返回,因此当您在viewDidLoad中访问它时,没有可访问的对象。您可能想尝试重新配置发出http请求的位置并创建weatherInfo。

答案 1 :(得分: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;
}