AFNetworking块和Feed

时间:2013-04-20 22:25:59

标签: ios uitableview objective-c-blocks afnetworking

好的我还有另一个问题,今晚,使用AFNetworking,我解析我的JSON Stream,一个添加对象MutableArray,当我插入尝试在成功块之外打印数组时,它表示null,但是在这个块里面它有效,所以如何将_listOfNewsArray传递给mainthread?

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

事实上,我发现解决方案,只需使用self.listOfNews,只需要考虑一下! -

答案 1 :(得分:0)

listOfNews的创建移出块并进入viewDidLoad,或将ivar变为块变量( _block NSM ....)。我更喜欢第一种解决方案。