iOS block循环引用

2023-09-17  本文已影响0人  小_梦

错误写法1

// ViewModel
@property (nonatomic, strong) UIViewController *controller;

// 在Viewcontroller中
@property (nonatomic, strong) VerityOtpViewModel *viewModel;

 self.viewModel.controller = self;
错误1.png

正确写法1

// ViewModel
@property (nonatomic, weak) UIViewController *controller;

// 在Viewcontroller中
@property (nonatomic, strong) VerityOtpViewModel *viewModel;

 self.viewModel.controller = self;;
正确1.png

错误写法2

[[_signInBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    [self.view endEditing:YES];
    self.viewModel.signInType = SignInTypeDictPassword;
}];
错误2.png

正确写法2

@weakify(self);
[[_signInBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    @strongify(self);
    [self.view endEditing:YES];
    self.viewModel.signInType = SignInTypeDictPassword;
}];
正确2.png

错误写法3

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeviceManagerCell *cell = [DeviceManagerCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.queryTrustDeviceModel = self.deviceLists[indexPath.section];
    cell.deleteBlock = ^{
        QueryTrustDeviceModel *subModel = self.deviceLists[indexPath.section];
        [self deleteAlert:subModel.model andWithDeleteModel:subModel];
    };
    return cell;
}
错误3.png

正确写法3

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeviceManagerCell *cell = [DeviceManagerCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.queryTrustDeviceModel = self.deviceLists[indexPath.section];
    @weakify(self);
    cell.deleteBlock = ^{
        @strongify(self);
        QueryTrustDeviceModel *subModel = self.deviceLists[indexPath.section];
        [self deleteAlert:subModel.model andWithDeleteModel:subModel];
    };
    return cell;
}
正确3.png

错误写法4

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer"];
    @weakify(self);
    cell.clickItemBlock = ^(CellModel * _Nonnull model) {
        @strongify(self);
        [self didSelectRowMehod:model tableView:tableView];
    };
    return cell;
}
错误4.png

正确写法4

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer"];
    @weakify(self);
    @weakify(tableView);
    cell.clickItemBlock = ^(CellModel * _Nonnull model) {
        @strongify(self);
        @strongify(tableView);
        [self didSelectRowMehod:model tableView:tableView];
    };
    return cell;
}
正确4.png

错误写法5

MRPinAlertConfig *config = [[MRPinAlertConfig alloc] init];
MRPinAlert *alert = [[MRPinAlert alloc] initWithConfig:config];
self.pinAlert = alert;
[alert show];
alert.pinBlock = ^(NSString * _Nonnull pin) {
    [self.pinAlert hide];
    self.viewModel.pin = pin;
    [self activateBiometryWithPin:pin];
};
alert.cancelBlock = ^{
    kStrongSelf(self)
    [self.tableView reloadData];
};
alert.forgotBlock = ^{
    kStrongSelf(self)
    [self.tableView reloadData];
};
错误5.png

正确写法5

MRPinAlertConfig *config = [[MRPinAlertConfig alloc] init];
MRPinAlert *alert = [[MRPinAlert alloc] initWithConfig:config];
self.pinAlert = alert;
[alert show];
@weakify(self)
alert.pinBlock = ^(NSString * _Nonnull pin) {
    @strongify(self);
    [self.pinAlert hide];
    self.viewModel.pin = pin;
    [self activateBiometryWithPin:pin];
};
alert.cancelBlock = ^{
    kStrongSelf(self)
    [self.tableView reloadData];
};
alert.forgotBlock = ^{
    kStrongSelf(self)
    [self.tableView reloadData];
};
正确5.png

注意: 如果block未访问self,不需要写@weakify(self),kStrongSelf(self)否则会出现警告“Unused variable 'self'”

上一篇 下一篇

猜你喜欢

热点阅读