ios中strong和weak的解释理解
来自stackoverflow解释的挺有意思的
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. When we use weak? The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).
weak/atrong主要是为了避免循环强引用 比如父类中含有子类{父类retain了子类},子类中又调用了父类{子类又retain了父类},这样都无法release
这是里面的关键。例子想想再写,先发表吧。会宿舍碎觉~
ios中strong和weak的解释理解的更多相关文章
- iOS 中strong,weak,copy,assign区别
1:ARC环境下,strong代替retain.weak代替assign2:weak的作用:在ARC环境下,,所有指向这个对象的weak指针都将被置为nil.这个T特性很有用,相信很多开发者都被指针指 ...
- ios中strong, weak, assign, copy
copy 和 strong(retain) 区别 1. http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是 ...
- IOS开发 strong,weak,retain,assign,copy nomatic 等的区别与作用
strong,weak,retain,assign,copy nomatic 等的区别 copy与retain:1.copy其实是建立了一个相同的对象,而retain不是:2.copy是内容拷贝,re ...
- iOS 之 Strong与Weak,_unsafe _unretained与weak区别
1. 在ARC中 strong(强引用) 相当于retain, weak(弱引用) 相当于assign.ARC下,strong告诉编译器自动插入retain.但是在ARC下,代理协议的属性依然用ass ...
- iOS strong 和weak的形象理解
转自:http://hi.baidu.com/phone_lwc/item/c36e5bfe1cf9c313ce9f32be 觉得讲的很容易理解 The difference is that an o ...
- @property中strong跟weak的区别
strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切 @property (nonatomic, strong) NSString *string1; @property ...
- iOS中respondsToSelector与conformsToProtocol的相关理解和使用
respondsToSelector相关的方法 : -(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例 -(BOOL) isMemberOfClass: ...
- 浅谈iOS中的单例模式
iOS中的单例模式 就我本身理解而言,我认为的单例:单例在整个工程中,就相当于一个全局变量,就是不论在哪里需要用到这个类的实例变量,都可以通过单例方法来取得,而且一旦你创建了一个单例类,不论你 ...
- IOS中@property的属性weak、nonatomic、strong、readonly等介绍
iOS开发中@property的属性weak nonatomic strong readonly等介绍 //property:属性://synthesize:综合; @property与@synthe ...
随机推荐
- hdu 5876 ACM/ICPC Dalian Online 1009 Sparse Graph
题目链接 分析:这叫补图上的BFS,萌新第一次遇到= =.方法很简单,看了别人的代码后,自己也学会了.方法就是开两个集合,一个A表示在下一次bfs中能够到达的点,另一个B就是下一次bfs中到不了的点. ...
- PDF firefox转换器
- BZOJ1778 [Usaco2010 Hol]Dotp 驱逐猪猡
首先我们列出转移矩阵$M$,$M_{i, j} = \frac {1 - \frac{p} {q}} {deg[i]}$(i,j之间有边)or $M_{i, j} = 0$(i,j之间没边) 则这个矩 ...
- SQLServer更新语句要注意
在SQLServer中 update语句中对于表不能使用别名 eg:update table a set a.column="" where ... 这样在SQLServer中是不 ...
- 在FireFox中修改cookie
一.安装Firebug插件(web开发中常用的调试工具) 1.打开菜单选择[附加组件] 2.搜索firebug,点击安装 3.安装完成 二.cookie的新建与编辑 1.选择cookies标签, ...
- iOS开发拓展篇—音效的播放
iOS开发拓展篇—音效的播放 一.简单介绍 简单来说,音频可以分为2种 (1)音效 又称“短音频”,通常在程序中的播放时长为1~2秒 在应用程序中起到点缀效果,提升整体用户体验 (2)音乐 比如游戏中 ...
- iframe自定义高度
function setIframeHeight() { var iframe=document.getElementById("iframe_id"); iframe.heigh ...
- 【转】Nginx 安装配置
Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. ...
- Oracle PL/SQL入门语法点
PL_SQL:带有分支和循环,面向过程匿名块:declare(可选,声明各种变量和游标的地方)begin(必要的,从此开始执行)exception(抓取到异常后执行的)end;[sql] view p ...
- ARM compiler No such file or directory
/********************************************************************************* * ARM compiler No ...