项目注意事项

2018-05-14  本文已影响7人  路飞_Luck
1.Block 的正确使用

如果在 block 里面只有一次用到 self, 则不需要强引用,否则需要强引用

__weak typeof(self) weakSelf = self;

/// 购买数量发生变化
_prodOptionView.qtyCountChangeBlock = ^(int qtyCount) {
    [weakSelf qtyCountChange:qtyCount];
};

/// Continue 按钮点击回调
_prodOptionView.continueBtnClickBlock = ^(int qtyStepCurrent, NSString *finalPrice, NSString *poaCode, NSString *cartFormat, NSString *stringFormat, NSString *lastPOAImgUrl, NSDictionary *newChartJson) {
    __strong __typeof(weakSelf) strongSelf = weakSelf;
    [strongSelf continueBtnClick:qtyStepCurrent withFinalPrice:finalPrice withPoaCode:poaCode withCartFormat:cartFormat withStringFormat:stringFormat withLastPOAImgUrl:lastPOAImgUrl prodJson:newChartJson];
    [strongSelf sendRecPosData];
};

如果在 block 中多次调用 self, 需要强引用 weakSelf, 因为 block 释放时间不确定,为了保证后面可以正确使用 self 而不出现野指针,所以需要使用 strongSelf 进行强引用

上一篇下一篇

猜你喜欢

热点阅读