`
ydbc
  • 浏览: 718326 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

iOS strong 和weak的形象理解

 
阅读更多

The difference is that an object will be deallocated as soon as there are nostrongpointers to it. Even if weak pointers point to it, once the last strong pointer is gone, the object will be deallocated, and all remaining weak pointers will be zeroed out.

Perhaps an example is in order.

Imagine our object is a dog, and that the dog wants to run away (be deallocated).

Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.

Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.

As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.

(weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放 ,即使还有weak型指针指向它。

一旦最后一个strong型指针离去 ,这个对象将被释放,所有剩余的weak型指针都将被清除。

可能有个例子形容是妥当的。

想象我们的对象是一条狗,狗想要跑掉(被释放)。

strong型指针就像是栓住的狗。只要你用牵绳挂住狗,狗就不会跑掉。如果有5个人牵着一条狗(5个strong型指针指向1个对象),除非5个牵绳都脱落 ,否着狗是不会跑掉的。

weak型指针就像是一个小孩指着狗喊到:“看!一只狗在那” 只要狗一直被栓着,小孩就能看到狗,(weak指针)会一直指向它。只要狗的牵绳脱落,狗就会跑掉,不管有多少小孩在看着它。

只要最后一个strong型指针不再指向对象,那么对象就会被释放,同时所有的weak型指针都将会被清除。


分享到:
评论

相关推荐

    iOS 中weak的实现代码示例

    只要学过 iOS 的人,都会对 strong、weak、copy等关键字应该都会很熟悉。weak 属性关键字就是弱引用,它不会增加引用计数但却能保证指针的安全访问,在对象释放后置为 nil,从而避免错误的内存访问。主要为了解决...

    IOS中(assign,retain,copy,weak,strong)的区别以及nonatomic的含义

    IOS中(assign,retain,copy,weak,strong)的区别以及nonatomic的含义 我们在声明@property 属性时,总是要在括号中写上assign、retain、copy、weak、strong中的一个,很多时候,我们仅仅只是按照习惯去写经常写...

    iOS Programming: The Big Nerd Ranch Guide (4th Edition) 2014 epub

    ARC and strong and weak references Handling touch events and gestures Toolbars, navigation controllers, and split view controllers Using Auto Layout to scale user interfaces Using Dynamic Type to ...

    iOS Programming: The Big Nerd Ranch Guide (4th Edition) (Big Nerd Ranch Guides)

    ARC and strong and weak references Handling touch events and gestures Toolbars, navigation controllers, and split view controllers Using Auto Layout to scale user interfaces Using Dynamic Type to...

    iOS开发实习面试题目

    weak,assign,strong的意义和区别? atomic,nonatomic的意义和区别? OC中有没有多继承?OC中怎么实现多继承功能? category和subclass的异同?平时怎样选择? OC中的变量是怎样存储的? block是怎样改变外界的变量的?...

    iOS面试题分享

    简述OC中内存管理机制。与retain配对使用的方法是dealloc还是release,为什么?需要与alloc配对使用的方法是dealloc还是...readwrite,readonly,assign,retain,copy,nonatomic 、atomic、strong、weak属性的作用

    iOS开发系列-ARC浅解

    在使用ARC开发过程中,开发者只需考虑strong / weak 的使用,不再需要考虑对象何时要retain,release/autorealease。使用ARC一般不会降低程序的效率。  二、ARC的工作原理  ARC一个很重要的原则是:只要某个对象...

    IOS property属性详细介绍使用注意事项

    IOS property属性 原子性 atomic nonatomic 访问权限 readWrite readOnly 内存管理 strong retain copy assign weak unsafe_unretained 方法命名 getter = setter = <name> 结论 1、原子性  (1...

    iOS 对象属性详细介绍

    retain,strong, copy,weak,assign,readonly, readwrite, unsafe_unretained 下面来分别讲讲各自的作用和区别: retain,计数器加1, (增加一个指向内存的指针) 对应release(计数器-1) setter 方法对参数进行 ...

    ios 5 ARC 完全指南

    介绍arc的不错的书籍,weak ,strong,unsafe_unretain ,block的介绍

    iOS内存管理

    强引用、弱引用 weak和strong只有你打开ARC时才会被要求使用

    iOS蓝牙开发数据实时传输

    随着iOS项目开发 很多app需要通过蓝牙与设备连接 蓝牙开发注意: 先定义中心设备和外围设备以及遵守蓝牙协议 @interface ViewController() @property (strong, nonatomic) CBCentralManager *manager; @property ...

    iOS 5.0 ARC开发详细解读

    最全面的ARC解读,手把手教会你。文档从基础到高级,从IBOutlet、Property、delloc开始讲解,重点讲解了weak、strong等的用法

    iOS常见的几个修饰词深入讲解

    主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

    iOS/OSX内存管理(二):借助工具解决内存问题

    上一篇博客《iOS/OSX内存管理(一):基本概念与原理》主要讲了iOS/OSX内存管理中引用计数和内存管理规则,以及引入ARC新的内存管理机制之后如何选择ownershipqualifiers(__strong、__weak、__unsafe_unretained和__...

    IOS 避免self循环引用的方法的实例详解

    IOS 避免self循环引用的方法的实例详解 示例代码: // - weak & strong #define myWeakify(VAR) \ try {} @finally {} \ __weak __typeof__(VAR) VAR##_myWeak_ = (VAR) #define myStrongify(VAR) \ try {} @...

    IOS 开发之查看大图的实现代码

    IOS 开发之查看大图的实现代码 本项目是取自传智播客的教学项目,加入笔者的修改和润饰。 1. 项目名称:查看大图 2. 项目截图展示 3. 项目功能 左右滑动查看图片 支持缩放功能 点击中间按钮移动图片 4. 项目代码 ...

    Swift4.0源代码最新201802

    20.自动引用计数(strong,weak,unowned).playground 21.检测API.playground 22.?对象的标识.playground 23.注释.playground 24.?元类型.playground 25.控件命名.playground 26.对象判等.playground 27.探究Self的用途...

    iOS图片界面翻页切换效果

    @property (weak, nonatomic) IBOutlet UIImageView *backgroundView; @property (strong,nonatomic) NSArray *array; @end @implementation ViewController -(NSArray *)array { if (_array == nil) { UIImage...

Global site tag (gtag.js) - Google Analytics