Changing an instance variable in a block

≡放荡痞女 提交于 2019-12-18 16:26:07

问题


I am quite confused about how to change an instance variable inside of a block.

The interface file (.h):

@interface TPFavoritesViewController : UIViewController {
    bool refreshing;
}

The implementation:

__weak TPFavoritesViewController *temp_self = self;
refreshing = NO;
[myTableView addPullToRefreshWithActionHandler:^{
    refreshing = YES;
    [temp_self refresh];
}];

As you might guess, I get a retain cycle warning when I try to change the refreshing ivar inside of the block. How would I do this without getting an error?


回答1:


Your assignment to refreshing is an implicit reference to self, it is shorthand for:

self->refreshing = YES;

hence the cycle warning. Change it to:

temp_self->refreshing = YES;


来源:https://stackoverflow.com/questions/11752500/changing-an-instance-variable-in-a-block

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