0

好的,今晚我还有一个问题,使用 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];
4

2 回答 2

1

事实上,我找到了解决方案,只是使用 self.listOfNews,只需要考虑一下!-

于 2013-04-21T14:12:17.297 回答
0

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

于 2013-04-21T12:57:33.050 回答