Handler中的数组计数

时间:2015-10-14 07:59:41

标签: ios objective-c iphone

我想在handler中获取arraycount,然后根据Count调用webservice .But数组计数给我数组。

__weak NewsViewController *self_ = self;
    [table addInfiniteScrollingWithActionHandler:^{
        NSLog(@"%lu",(unsigned long)[tableDataArray count]); // This Line give error Capturing self strongly in this block lead to retain cycle;
        [self_ callWebService];
    }];

2 个答案:

答案 0 :(得分:1)

尝试创建一个指向tableDataArray的弱指针(如自我)

__weak typeof(NSMutableArray*) _w_tableDataArray = tableDataArray;
__weak NewsViewController *self_ = self;
[table addInfiniteScrollingWithActionHandler:^{
        NSLog(@"%lu",(unsigned long)[_w_tableDataArray count]); // This Line give error Capturing self strongly in this block lead to retain cycle;
        [self_ callWebService];
    }];

答案 1 :(得分:1)

虽然你正在创造一个弱小的自我,但你实际上并没有在街区中引用它。当您致电[tableDataArray count];时,相当于在您的情况下致电self.tableDataArray,您应该致电self_.tableDataArray;

为清楚起见,其示例性用途如下:

...
@property (nonatomic, strong) NSMutableArray *tableDataArray;
...
...

__block __weak NewsViewController *welf = self;
[table addInfiniteScrollingWithActionHandler:^{
    NSLog(@"%li", welf.tableDataArray.count);
    [welf callWebService];
}];

welf代表弱者。