iOS 处理成员变量(即仅使用_调用属性)的循环引用问题

如果你的成员变量并不是使用属性声明的,如这种形式

@interface TZImagePickerController () {
    NSTimer *_timer;
    UILabel *_tipLabel;
    UIButton *_settingBtn;
    BOOL _pushPhotoPickerVc;
    BOOL _didPushPhotoPickerVc;
    
    UIButton *_progressHUD;
    UIView *_HUDContainer;
    UIActivityIndicatorView *_HUDIndicatorView;
    UILabel *_HUDLabel;
    
    UIStatusBarStyle _originStatusBarStyle;
}

我们无法使用 self. 的形式调用成员变量,只能通过 _ 的形式 调用成员变量,那么如何处理 循环引用 问题?

解决方案

__weak typeof(self)weakSelf = self;
[[TZImageManager manager] getCameraRollAlbum:self.allowPickingVideo allowPickingImage:self.allowPickingImage completion:^(TZAlbumModel *model) {
   __strong typeof(weakSelf) strongSelf = weakSelf;
   strongSelf->_didPushPhotoPickerVc = YES;
 }];


说明: 在 block 里一定要使用 strongSelf ,否则会报错。

Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first

大概意思就是 不允许使用__weak 指针,因为在某些情况下可能会出现 null 值,所以要使用 强引用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容