问题
I'm new to ARC. I want to call a method in a complete block, but I get the warning: Capturing 'self' strongly in this block is likely to lead to a retain cycle.. Code:
- (void) handlerComplete
{
//...
}
- (void) loadData
{
...
operation.completeBlock = ^(NSInteger index) {
[self handlerComplete];
};
}
Any advice? Thanks.
回答1:
Try with
- (void) loadData
{
__weak MyClassType *myClass = self;
operation.completeBlock = ^(NSInteger index) {
[myClass handlerComplete];
};
}
回答2:
Make a weak reference instead:
operation.completeBlock = ^(NSInteger index) {
__weak Foo *bar = self;
[bar handlerComplete];
};
I think this will work, though I haven't verified it.
来源:https://stackoverflow.com/questions/9835476/call-a-method-in-a-block