我正在尝试将正确返回的 JSON 的结果放入 tableview 中。问题是我只能访问返回信息的块内的数据,甚至将结果分配给实例变量。我的代码如下:
NSURL *url = [NSURL URLWithString:@"http://www.my_url.com.br/app/?type=list&info=15,16&lat=some_data&long=some_data"];
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:url];
ASIHTTPRequest *request = _request;
request.requestMethod = @"POST";
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(@"Response: %@", responseString);
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:request.responseData options:0 error:nil];
self.data = [root objectForKey:@"listing"];
NSLog(@"Data returned: %@", data); //Everything works well. The data returned is printed correctly
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request startAsynchronous];
NSLog(@"Data only: %@", data); //At this point, the "data" content is nil
这是我的“ListDataController.h”文件定义:
@interface ListDataController : UITableViewController{
ApplicationCell *tmpCell;
NSArray *data;
UILabel *status ;
UINib *cellNib;
}
@property (nonatomic, retain) IBOutlet ApplicationCell *tmpCell;
@property (nonatomic, retain) NSArray *data;
返回的 JSON:数据自身:({ Icon = "Baseball.png"; Name = Baseball; NumRatings = 106; Price = "$2.98"; Publisher = "Super Sportz, Inc."; Rating = "3.5"; }, {图标 = “Checkers.png”;名称 = Checkers;NumRatings = 87;价格 = 免费;出版商 =“Gameitoids, Inc.”;评级 = 4;})
问题是:为什么我无法访问块外的实例变量“数据”,甚至将其分配为 json 的结果?