我想解析一個使用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不起作用。
我用'@interface ViewController(){ __block WeatherClasses * weather; __block NSDictionary * weatherDict; } @end '它的標籤仍顯示0 – highboi
這是在viewcontroller.m – highboi