iOS __block和__weak的区别
2017-07-19 22:47
337 查看
//联系人:石虎
QQ: 1224614774昵称:嗡嘛呢叭咪哄
API Reference对__block变量修饰符有如下几处解释:
大概意思归结出来就是两点:
1.__block对象在block中是可以被修改、重新赋值的。
2.__block对象在block中不会被block强引用一次,从而不会出现循环引用问题。
API Reference对__weak变量修饰符有如下几处解释:
使用了__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。所以尽可能不要使用这个修饰符
谢谢!!!
QQ: 1224614774昵称:嗡嘛呢叭咪哄
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。所以尽可能不要使用这个修饰符
谢谢!!!
相关文章推荐
- iOS开发63-__block 和 __weak的区别
- iOS开发之 __block 与 __weak的区别理解
- iOS学习笔记29-__block 与 __weak的区别理解
- iOS-Delegate-Block-NSNotification的区别和使用
- iOS中assign与weak,retain与strong的区别
- __block和__weak修饰符的区别
- iOS下的__block与__weak
- IOS 看懂此文,你的block再也不需要WeakSelf弱引用了!
- iOS海哥开发笔记 (海哥原创,retain、strong、weak、assign区别)
- 一篇文章让你看懂IOS中的block为何再也不需要WeakSelf弱引用
- iOS中assign,copy,retain之间的区别以及weak和strong的区别
- __block 和 __weak 区别
- 【IOS学习】iOS——Block中 __block、__weak 、typeof、define等词的小解释
- iOS - property,strong,weak,retain,assign,copy,nomatic 的区别及使用
- __block 与 __weak的区别理解
- iOS __weak 和 __block 的使用探讨
- iOS开发-retain/assign/strong/weak/copy/mutablecopy/autorelease区别
- iOS中block的用法 以及和函数用法的区别
- __block 与 __weak的区别理解
- iOS中 assign 和 weak 修饰符的区别