好的,今晚我还有一个问题,使用AFNetworking,我解析了JSON流,添加了一个对象MutableArray,当我插入尝试在成功块之外打印数组时,它说为null,但是在此块中它可以工作,所以如何将_listOfNewsArray传递到主线程中?
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"bgWhitelight" ofType:@"png"];
self.tableView.backgroundColor = [[UIColor alloc] initWithPatternImage:[[UIImage alloc] initWithContentsOfFile:path]];
NSURLRequest *newsRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://aXXXXXXXXXXXipt/beta.php"]];
AFJSONRequestOperation *newsJSONRequest = [AFJSONRequestOperation JSONRequestOperationWithRequest:newsRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSArray *newsArray = [JSON objectForKey:@"news"];
_listOfNews = [[NSMutableArray alloc]init];
for (NSDictionary *oneNews in newsArray) {
CCENews *currentNews = [[CCENews alloc]init];
currentNews.title = [oneNews objectForKey:@"title"];
currentNews.content = [oneNews objectForKey:@"content"];
currentNews.category = [currentNews getHiResCategoryPicture:[oneNews objectForKey:@"category"]];
currentNews.date = [oneNews objectForKey:@"date"];
currentNews.imageURL = [oneNews objectForKey:@"pictureurl"];
[_listOfNews addObject:currentNews];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", error);
}];
[newsJSONRequest start];
最佳答案
实际上,我只是使用self.listOfNews找到了解决方案,只需要考虑一下!