因此,我尝试在线访问json文件,在使用AFHTTPRequestOperation方法运行完成块时,我可以获取JSON文件的内容。但是,当我试图在完成块之外执行NSLog时,它将变为null。我想知道为什么会发生这种事,我会如何解决这个问题。下面是我到目前为止拥有的代码:
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCredential:credentials];
[operation setResponseSerializer:[AFJSONResponseSerializer alloc]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
jsonOutputData = [NSMutableArray arrayWithObject:responseObject];
self.newsFeedArray = [NSMutableArray arrayWithArray:[jsonOutputData copy]];
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Failure: %@", error);
}];
[manager.operationQueue addOperation:operation];
NSLog(@"%@", self.newsFeedArray);成功的NSLog显示JSON,而带有self.newsFeedArray的NSLog显示(null)。我只是想知道为什么会这样。如果你需要更多的澄清,请告诉我。任何帮助都将不胜感激!
谢谢!
发布于 2015-03-27 19:47:26
成功和失败块被异步调用。因此,self.newsFeedArray在上次NSLog调用时还没有被设置,因为操作还没有完成。
您可以等待操作完成(例如,[operation waitUntilFinished]),但是您几乎肯定不想这样做,特别是在主线程上。而是让完成块触发所需的行为。
https://stackoverflow.com/questions/29308994
复制相似问题