call a method in a block?

两盒软妹~` 提交于 2019-12-22 18:24:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!