strong & weak
Here is a quick summary: A strong reference will keep the object it points to from being deallocated. A
weak reference will not. Thus instance variables and properties that are marked as weak are pointing at
objects that might go away. If this happens, that instance variable or property will be set to nil, instead
of continuing to point to where the object used to live.
Most strong reference cycles can be broken down into a parent-child relationship. A parent typically
keeps a strong reference to its child, so if a child needs a pointer to its parent, that pointer must be a
weak reference to avoid a strong reference cycle.
A child holding a strong reference to its parent’s parent also causes a strong reference cycle. So the
same rule applies in this situation: if a child needs a pointer to its parent’s parent (or its parent’s
parent’s parent, etc.), then that pointer must be a weak reference.
Apple’s development tools includes a Leaks tool to help you find strong reference cycles.
Memory management attribute
The memory management attribute’s values are strong, weak, copy, and unsafe_unretained. This
attribute describes the type of reference that the object with the instance variable has to the object that
the variable is pointing to.
For properties that do not point to objects (like the int valueInDollars), there is no need for memory
management, and the only option is unsafe_unretained. This is direct assignment. You may also see
the value assign in some places, which was the term used before ARC.
(The “unsafe” part of unsafe_unretained is misleading when dealing with non-object properties. It
comes from contrasting unsafe unretained references with weak references. Unlike a weak reference,
an unsafe unretained reference is not automatically set to nil when the object that it points to is
destroyed. This is unsafe because you could end up with dangling pointers. However, the issue of
dangling pointers is irrelevant when dealing with non-object properties.)
As the only option, unsafe_unretained is also the default value for non-object properties, so you can
leave the valueInDollars property as is.
For properties that manage a pointer to an Objective-C object, all four options are possible. The
default is strong. However, Objective-C programmers tend to explicitly declare this attribute. (One
reason is that the default value has changed in the last few years, and that could happen again.)
When a
property points to an instance of a class that has a mutable subclass (like NSString/NSMutableString
or NSArray/NSMutableArray), you should set its memory management attribute to copy.
Why is it safer to do this for NSString? It is safer to make a copy of the object rather than risk
pointing to a possibly mutable object that could have other owners who might change the object
without your knowledge.
In terms of ownership, copy gives you a strong reference to the object pointed to. The original string
is not modified in any way: it does not gain or lose an owner, and none of its data changes.
While it is wise to make a copy of an mutable object, it is wasteful to make a copy of an immutable
object. An immutable object cannot be changed, so the kind of problem described above cannot
occur. To prevent needless copying, immutable classes implement copy to quietly return a pointer to
the original and immutable object.
Note that if you implement both a custom setter and a custom getter (or just a custom getter on a readonly
property), then the compiler will not create an instance variable for your property. If you need
one, you must declare it yourself.
Declaring a property in a class interface only declares the accessor methods in a class interface. In
order for a property to automatically generate an instance variable and the implementations for its
methods, it must be synthesized, either implicitly or explicitly. Properties are implicitly synthesized
by default. A property is explicitly synthesized by using the @synthesize directive in an
implementation file
strong & weak的更多相关文章
- 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- ARC声明属性关键字详解(strong,weak,unsafe_unretained,copy)
ARC声明属性关键字详解(strong,weak,unsafe_unretained,copy) 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都很熟悉了, ...
- 对于atomic nonatomic assign retain copy strong weak的简单理解
atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环 ...
- IOS—— strong weak retain assign 学习
转自:http://wenzongliang.iteye.com/blog/1746604 简单讲strong等同retain weak比assign多了一个功能,当对象消失后自动把指针变成nil,好 ...
- @property中的copy.strong.weak总结
1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...
- assign,copy,strong,weak,nonatomic的理解
举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作: 1 在堆上分配一段 ...
- property attribute: assign, strong, weak, unsafe_unretain and copy
assign:用于“纯量类型”(如CGFloat 或 NSInteger等): strong:用于“对象类型”,定义了一种“拥有关系”(owning relationship),为这种属性设置新值时, ...
- ios copy/strong/weak..使用总结
总结 关于属性的这些选项的学习,做一下总结: 所有的属性,都尽可能使用nonatomic,以提高效率,除非真的有必要考虑线程安全. NSString:通常都使用copy,以得到新的内存分配,而不只是原 ...
- [iOS基础控件 - 6.12.3] @property属性 strong weak copy
A.概念 @property 的修饰词 strong: 强指针/强引用(iOS6及之前是retain) weak: 弱智真/弱引用(iOS6及之前是assign) 默认情况所有指针都是强指针 ...
随机推荐
- 替换Notes,K2 BPM为vivo打造新平台
vivo是步步高旗下的分品牌,专注于智能手机领域,隶属于广东步步高工业电子有限公司.vivo追求乐趣.充满活力.年轻时尚的群体一起打造拥有卓越外观.专业级音质.极致影像.愉悦体验的智能产品,并将敢于追 ...
- Wince 6.0 窗口最大化显示
在InitDialog用如下代码实现: CRect m_FullScreenRect; //全屏区域 CRect WindowRect; GetWindowRect(&Window ...
- juqey.html(),text(),val()
.html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...
- LVDS,MIPI,EDP
一.背景介绍: 随着显示分辨率的越来越高,传统的VGA.DVI等接口逐渐不能满足人们的视觉需求.随后就产生了以HDMI.DisplayPort为代表的新型数字接口,外部接口方面HDMI占据了较大市场优 ...
- ubuntu 14.04 安装docker
常会遇到的问题就是网络的问题,如访问https://get.docker.io/ 遇到403的问题:总结一下最简单的几条命令: $ sudo apt-get install apt-transport ...
- 对编写html代码的几点儿小建议
1.DOCTYPE说明:告诉浏览器要使用哪种规范来解释该文档内容: <!DOCTYPE html PUBLIC "-W3//DTD//XHTML 1.0 Transitional// ...
- linux命令:find
1.命令介绍: find用来在整个系统指定的路径下搜索文件,功能强大,但是遍历整个系统时很耗时间. 2.命令格式: find 路径 [选项] [print -exec -ok...] 3.命令参数: ...
- Uri.AbsoluteUri 与 Uri.ToString() 的区别
UriBuilder builder = new UriBuilder("http://somehost/somepath"); builder.Query = "som ...
- apply()和call()和bind()
1.方法定义 call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function ...
- 【我所理解的Cocos2d-x】第六章 精灵Sprite 读书笔记
简介: 精灵是2D游戏里最重要的元素.游戏场景中大部分可见的元素都直接或间接地与精灵相关. 在Cococs2d-xz中,精灵使用Sprite表示,它将一张纹理的一部分或者全部的矩形区域绘制在屏幕上. ...