我想获得一个NSArray的参考,当我调用sendAsyncrhonousRequest时会通过。一旦我有了NSArray,我想将它分配给一个类属性,但似乎我不能那样做。分配来自匿名处理程序的数据ios
@implementation BarTableViewController {
NSArray *_jsonArray;
}
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 200 && data.length > 0 && error == nil)
{
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
_jsonArray = jsonArray; // this doesn't work? _jsonArray is at the class level
}
}
else if (error)
{
NSLog(@"HTTP Status: %ld", (long)statusCode);
}
else if (statusCode != 200)
{
NSLog(@"HTTP Status: %ld", (long)statusCode);
}
}];
如果我遍历jsonArray它将正确显示数据。如果将其分配给_jsonArray以稍后使用它,则不再返回任何数据。该数组的数量为零。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _jsonArray.count; // always returns zero
}
如何将jsonArray分配给类属性,以便日后可以使用该数据?
显示你的_jsonArray声明是否声明它很弱?你能显示代码来演示你如何尝试读取数组并找到它是空的吗?它是空的还是零?另外,至少从我的经验来看,你的词汇有点独特。他们通常被称为块,而不是匿名处理程序;通常我们会调用一个实例变量而不是类级别,以及属性和属性。只是一个友好的提示。 – 2013-03-22 07:21:09
我是新来的客观的C/IOS,所以我的行话是关闭的,对此感到遗憾。我已经更新了这个问题,并提供了您要求的内容。当numberOfRowsInSection被调用时,计数总是为零。 – user1218776 2013-03-22 07:25:47
没问题:)你确定它是相同的实例,它设置它与读取它? – 2013-03-22 07:27:13