2013-04-20 110 views
0

好吧我今晚有另一个问题,使用AFNetworking,我解析我的JSON流,添加对象MutableArray,当我插入尝试打印数组以外的成功块,它说空,但里面这块它的工作原理,所以我怎么能通过_listOfNewsArray到主线程?AFNetworking块和饲料

这是我的代码:

- (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]; 

回答

1

事实上,我找到了解决方案,只是使用self.listOfNews,只是想一想! -

0

移动的listOfNews创造出块并进入viewDidLoad中,或者使一个实例变量块变量( _block NSM ....)。我更喜欢第一个解决方案。