The difference is that an object will be deallocated as soon as there are no strong pointers 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型指针都将会被清除。

參考:http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5

ios weak和strong的差别的更多相关文章

  1. 【iOS atomic、nonatomic、assign、copy、retain、weak、strong】的定义和区别详解

    一.atomic与nonatomic 1.相同点 都是为对象添加get和set方法 2.不同点 atomic为get方法加了一把安全锁(及原子锁),使得方法get线程安全,执行效率慢 nonatomi ...

  2. iOS之weak和strong、懒加载及循环引用

    一.weak和strong 1.理解 刚开始学UI的时候,对于weak和strong的描述看得最多的就是“由ARC引入,weak相当于OC中的assign,但是weak用于修饰对象,但是他们都不会造成 ...

  3. iOS - property,strong,weak,retain,assign,copy,nomatic 的区别及使用

    1:ARC环境下,strong代替retain.weak代替assign,xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的.xcod ...

  4. iOS retain、strong、weak、assign

    iOS retain.strong.weak.assign strong与weak是由ARC新引入的对象变量属性xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和a ...

  5. weak nonatomic strong等介绍(ios)

    @property的属性weak nonatomic strong等介绍(ios) 2014-12-02 18:06 676人阅读 评论(0) 收藏 举报 学习ios也已经快半个月了,也尝试做简单的应 ...

  6. 【IOS学习基础】weak和strong、懒加载、循环引用

    一.weak和strong 1.理解 刚开始学UI的时候,对于weak和strong的描述看得最多的就是“由ARC引入,weak相当于OC中的assign,但是weak用于修饰对象,但是他们都不会造成 ...

  7. iOS中assign,copy,retain之间的区别以及weak和strong的区别

    @property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...

  8. 从一个弱引用导致的奔溃 谈 weak assign strong的应用场景【iOS开发教程】

    从一个弱引用导致的奔溃 谈 weak assign strong的应用场景 .h中的定义方法一: @property (nonatomic, assign) NSArray *dataSource; ...

  9. iOS开发——基础篇——assign,copy,retain之间的区别以及weak和strong的区别

    @property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...

随机推荐

  1. S - New Year Transportation

    Problem description New Year is coming in Line World! In this world, there are n cells numbered by i ...

  2. Java 介绍比较全面的一遍文章

    Java简介 Java是由Sun Microsystems公司于1995年5月推出的Java程序设计语言(以下简称Java语言)和Java平台的总称.用Java实现的HotJava浏览器(支持Java ...

  3. 编写linux 命令行实用工具 shell命令

    今天我想以带着问题的方法学习新的技术. 问题1: 编写一个命令 语法: command  options  path  expressions

  4. MySQL学习笔记之左连接

     MySQL的左连接 #左连接,以左表为基表 select class1.stuid,class1.stuname,sex,course from class1 left join course on ...

  5. Php.ini文件位置在哪里 Php.ini文件找不到

    转载自:http://www.php100.com/html/php/rumen/2013/0831/26.html [导读] Php ini文件是php的一个配置文件,在windows主机中如果你未 ...

  6. PHP 框架Laravel Eloquent 实现身份验证

    PHP自从5.3后似乎又热度又回升, 最近了解了一下PHP框架之一Laravel, 最近最新的版本已经是4.3  基本的结构这里不讲, 要了解可以在这里看文档 http://v4.golaravel. ...

  7. Assembly之instruction之Indirect Autoincrement Mode

    Assembler Code Content of ROMMOV @R10+,0(R11)   MOV @R10+,0(R11) Length: One or two words Operation: ...

  8. 转:Python结合P有winauto进行windows UI自动化

    https://blog.csdn.net/z_johnny/article/details/52778064

  9. Yoga710笔记本Win10和Ubuntu系统共存

    联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...

  10. Arduino控制继电器模块

    一.实物图 二.例子代码 每隔5s切换断开 接通状态