美文网首页
项目注意事项

项目注意事项

作者: 路飞_Luck | 来源:发表于2018-05-14 17:51 被阅读7次
    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 进行强引用

    相关文章

      网友评论

          本文标题:项目注意事项

          本文链接:https://www.haomeiwen.com/subject/jhyjdftx.html