Ios AppDelegate中初始化的字典在UIViewController中的值为零
我使用OpenWeatherAPI获取实时天气数据,并将其显示在UIViewController中。但是,我在AppDelegate中发出http请求。因此,我在AppDelegate中用一个名为weatherForcast()的方法发出了API请求,将JSON响应转换为一个NSDictionary对象,并将该对象打印到控制台,以确保一切正常工作,它做到了Ios AppDelegate中初始化的字典在UIViewController中的值为零,ios,objective-c,nsdictionary,appdelegate,openweathermap,Ios,Objective C,Nsdictionary,Appdelegate,Openweathermap,我使用OpenWeatherAPI获取实时天气数据,并将其显示在UIViewController中。但是,我在AppDelegate中发出http请求。因此,我在AppDelegate中用一个名为weatherForcast()的方法发出了API请求,将JSON响应转换为一个NSDictionary对象,并将该对象打印到控制台,以确保一切正常工作,它做到了 NSString *urllink = [NSString stringWithFormat:@"http://api.openweathe
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中打印同一字典时相比,结果是零。发生了什么事 调用viewDidLoad时,weatherInfo可能尚未初始化。如果需要http调用,则数据可能尚未返回,因此在viewDidLoad中访问数据时,没有要访问的对象。您可能希望尝试重新配置发出http请求和创建weatherInfo的位置 当您创建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;
}
哦,好吧,我明白你的意思了。但是我认为appDelegate方法在应用程序启动之前就已经出现了?如果您发出http请求,然后在回调块中处理返回的数据,那么viewDidLoad可能会在回调之前被调用。好的。我将尝试将代码迁移到其他地方。一个选项(有很多)是在viewDidLoad中发出http请求,然后在ViewController类中处理返回数据。在这种情况下,weatherInfo将是ViewController的一个属性。