__blick 和 __weak的区别

API Reference对__block变量修饰符有如下几处解释:

//A powerful feature of blocks is that they can modify
variables in the same lexical scope. You signal that a block
can modify a variable using the __block storage type
modifier.

//At function level are __block variables. These are mutable
within the block (and the enclosing scope) and are preserved
if any referencing block is copied to the heap.
大概意思归结出来就是两点:
1.__block对象在block中是可以被修改、重新赋值的。
2.__block对象在block中不会被block强引用一次,从而不会出现循环引用问题。

API Reference对__weak变量修饰符有如下几处解释:

__weak specifies a reference that does not keep the
referenced object alive. A weak reference is set to nil when
there are no strong references to the object.
使用了__weak修饰符的对象,作用等同于定义为weak的property。自然不会导致循环引用问题,因为苹果文档已经说的很清楚,当原对象没有任何强引用的时候,弱引用指针也会被设置为nil。

因此,__block和__weak修饰符的区别其实是挺明显的:
1.__block不管是ARC还是MRC模式下都可以使用,可以修饰对象,还可以修饰基本数据类型。
2.__weak只能在ARC模式下使用,也只能修饰对象(NSString),不能修饰基本数据类型(int)。
3.__block对象可以在block中被重新赋值,__weak不可以。
PS:__unsafe_unretained修饰符可以被视为iOS SDK 4.3以前版本的__weak的替代品,不过不会被自动置空为nil。所以尽可能不要使用这个修饰符。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="new_file.vue"></script> </head> <body> <div id="app"> <button @click="reset">还原</button> <div v-noData="{goodsList}"> <ul> <li><span>商品名称</span><span>价格</span><span>操作</span></li> <li v-for="(good,index) in goodsList" :key="index"> <span>{{good.name}}</span> <span>{{good.price|filterPrice }}</span> <span @click="delood(index)" style="color: red;cursor: pointer;">删除</span> </li> </ul> </div> </div> <script> const goodsMixin = { data(){ return{ goodsList:[ {name:'电饭煲',price:200.133232}, {name:'电视机',price:880.998392}, {name:'电冰箱',price:650.2034}, {name:'电脑',price:4032.9930}, {name:'电磁炉',price:210.4322} ] } }, methods: { delGood(index){ this.goodsList.splice(index,1) }, reset(){ this.goodsList = [ {name:'电饭煲',price:200.133232}, {name:'电视机',price:880.998392}, {name:'电冰箱',price:650.2034}, {name:'电脑',price:4032.9930}, {name:'电磁炉',price:210.4322} ] } }, filters:{ filterPrice(value){ let newValue = [] newValue = value.toLocaleString().split('.') newValue[0] = (newValue[0] + '').replace(/\d{1,3}(?=(\d{3})+$)/g,'$&') return newValue[0] + '.' + newValue[1]/slice(0,2); } } } new Vue({ el:'app', mixins:[goodsMixin], directives:{ nodata:{ inserted(el,binding){ const noData = document.createElement('div'); noData.classList = 'noData' noData.innerHTML = '暂无数据'; el.appendChild(noData) const{goodsList} = binding.value; noData.style.display = goodsList.length === 0 ?'blick' : 'none' }, updata(el,binding){ const{goodsList} = binding.value; el.querySelector('.noData').style.display = goodsList.length === 0 ?'blick' : 'none'; } } }, data(){ return{} } }) </script> <style> #app ul li{ list-style: none; width: 400px; display: flex; justify-content: space-between; } .noData{ width: 400px; text-align: center; margin-left: 40px; } </style> </body> </html>纠错
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值