我正在尝试使用 AFNetworking 和 MJExtensions 解析 JSON 链接。查看代码中的注释。
- (void)viewDidLoad {
[super viewDidLoad];
NSString *apiURL = @"https://api.forecast.io/forecast/APIKEY/Lat,Long";
NSURL *URL = [NSURL URLWithString:apiURL];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
weatherDict = (NSDictionary *)responseObject;
weather = [WeatherClasses mj_objectWithKeyValues:weatherDict];
//----------------------
//when i set the label.text here, the label shows whatever the JSON value is
self.apparentTemperature.text = [NSString stringWithFormat:@"%f", weather.currently.apparentTemperature];
NSLog(@"Temp: %f", weather.currently.apparentTemperature);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
//--------------------------
//but if i set the label here, its always 0 (or nil for NSString)
self.apparentTemperature.text = [NSString stringWithFormat:@"%f", weather.currently.apparentTemperature];
}
在成功块中,我的字典有数据,但是当我退出成功块时,字典为零。我怎样才能使字典在 AFHTTPSessionManager 中设置后不为零?
使用 __block 不起作用。