iOS strong 和weak的形象理解
转自:http://hi.baidu.com/phone_lwc/item/c36e5bfe1cf9c313ce9f32be
觉得讲的很容易理解
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型指针都将会被清除。
【】我服了!!!
iOS strong 和weak的形象理解的更多相关文章
- iOS Strong 和 weak
iOS 5 中对属性的设置新增了strong 和weak关键字来修饰属性(iOS 5 之前不支持ARC) strong 用来修饰强引用的属性: @property (strong) SomeClass ...
- iOS strong与weak的使用
strong修饰的属性是强指针类型的,weak修饰的属性是弱指针类型的 ARC对于内存中的对象管理机制,当某个对象没有被强指针指向的时候,该对象就会被销毁. 所以不适当的使用strong和weak修饰 ...
- IOS strong和weak的区别
strong和weak的区别 strong表示保留它指向的堆上的内存区域不再指向这块区域了. 也就是说我强力指向了一个区域,我们不再指向它的条件只有我们指向nil或者我自己也不在内存上,没有人stro ...
- ios中strong和weak的解释理解
来自stackoverflow解释的挺有意思的 Imagine our object is a dog, and that the dog wants to run away (be dealloca ...
- IOS开发 strong,weak,retain,assign,copy nomatic 等的区别与作用
strong,weak,retain,assign,copy nomatic 等的区别 copy与retain:1.copy其实是建立了一个相同的对象,而retain不是:2.copy是内容拷贝,re ...
- iOS内存管理retain,assign,copy,strong,weak
转自:http://www.cnblogs.com/nonato/archive/2013/11/28/3447162.html iOS的对象都继承于NSObject, 该对象有一个方法:retain ...
- 对于atomic nonatomic assign retain copy strong weak的简单理解
atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环 ...
- iOS - property,strong,weak,retain,assign,copy,nomatic 的区别及使用
1:ARC环境下,strong代替retain.weak代替assign,xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的.xcod ...
- iOS retain、strong、weak、assign
iOS retain.strong.weak.assign strong与weak是由ARC新引入的对象变量属性xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和a ...
随机推荐
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- SQL Server2005安装配置以及测试
SQL Server2005有2种版本,一种是集成版的, 一种是2个文件夹形式的.这里使用后者,安装文件夹名字为:SQL Server x86,该文件夹里面有Servers和Tools文件夹以及一些其 ...
- 【原】Storm分布式RPC
5. Storm高级篇 序列化 分布式RPC High level overview LinearDRPCTopologyBuilder Local mode DRPC Remote mode DRP ...
- asp.net(class0625)
1 SiteMapPath 面包屑导航控件 要想使用这个控件,必须创建一个站点地图,也就是 web.sitemap web.sitemap是一个xml文件: 根节点必须是:<siteMap> ...
- 集群——LVS理论(转)
原文:http://caduke.blog.51cto.com/3365689/1544229 当单个服务器性能 不能满足日益增多访问流量时,服务器的扩展策略: Scale Up :向上扩展,提升单个 ...
- codeforce 630N Forecast
N. Forecast time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input ...
- 如何设置(修改)jetty(maven插件maven-jetty-plugi)的端口
在使用jetty的maven插件,有两种方式来改变jetty server的端口,第一种方式较为简单,即: 通过命令行指定端口:mvn -Djetty.port=9999 jetty:run 另一种方 ...
- UVaLive 7371 Triangle (水题,判矩形)
题意:给定两个三角形,问你能不能拼成矩形. 析:很明显,要想是矩形,必须是四个角是直角,那么三角形必须是直角三角形,然后就是只能斜边相对,然后呢?就没了. 代码如下: #pragma comment( ...
- input type=“submit”屏蔽自带的提交事件
<p><input type="submit" class="submit" value="确认支付" onclick=& ...
- CSS 的 appearance 属性
在 Firefox 1.0+,Chrome 1.0+ 和 Safari 3.0+ 中,我们可以使用 -moz-appearance 或者 -webkit-appearance 属性让元素模仿系统原生控 ...