1

I'm trying to load data from API and show it inside a UITableView. I used AFNetworking for the network calls, but now I'm facing a problem: I can't access myTableView nor self inside the success block.

@property (weak, nonatomic) IBOutlet UITableView *myTableView; 
@property (nonatomic,retain) NSMutableArray *myDataSource;

@synthesize featuredProductsTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NetworkManager *networkManager = [NetworkManager  getInstance];
    myDataSource = [NSMutableArray array]; 

    [networkManager getPath:@"example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        [myDataSource addObjectsFromArray:JSON];

        [myTableView reloadData]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

Debugging the code shows that inside the block I can't access any of self, myDataSource, or myTableView.

How can I solve this issue?

Thanks.

4

2 回答 2

0

您可能应该使用:

__block myDataSource = [NSMutableArray array];

您还可以阅读:

“__block”关键字是什么意思?

于 2013-04-25T15:01:57.440 回答
-2

在你的块之前声明这个:

__block typeof(self) this = self;

至少,这将允许您self通过调用它从块内部到达this

编辑:那不是要走的路,我的错误。请参阅实现 API 时如何避免在块中捕获自我?

于 2013-04-25T14:45:53.387 回答