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) 默认情况所有指针都是强指针 ...
随机推荐
- 关于struts2.0 中 struts.xml设置了struts.devMode 的值为TRUE后仍然不起作用的分析
首先确认jdk 和tomcat的环境变量是否配置正确. 下面是配置方式 jdk的环境变量配置步骤: 安装j2sdk以后,需要配置一下环境变量,在我的电脑->属性->高级->环境变量- ...
- 判断CAD版本
使用命令: ACADVER ACADVER = "17.2s (LMS Tech)" (只读) CAD2016 ACADVER = "20.1s (LMS Tech)&q ...
- JDBCl链接中Statement
作用:创建的Statement对象执行SQL语句 (1)对象有Connection对象调用createStatement()方法创建 (2)有Statement对象调用executeUpdate()方 ...
- java.lang.ClassNotFoundException: Didn't find class "*****(转载)
很多人出现了java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{*****Activity}: java. ...
- linux 下find命令 --查找文件名
1.在某目录下查找名为"elm.cc"的文件 find /home/lijiajia/ -name elm.cc 2.查找文件名中包含某字符(如"elm")的文 ...
- XListView刷新
package com.example.da; import java.util.ArrayList;import java.util.List; import com.badu.net.Networ ...
- 几种循环语句 ,break,continue语句用法
Java有非常灵活的三循环机制.可以使用以下三种循环之一: while 循环 do...while 循环 for 循环while循环是一个控制结构,可以重复的特定任务次数.在执行时,如果布尔表达式的结 ...
- 第七课第一节,T语言流程语句( 版本5.0)
流程语句 if语句 用if语句可以构成分支结构.它根据给定的条件进行判断,以决定执行某个分支程序段.TC综合开发工具的if语句有三种基本形式,并且每个语句的结尾都要有一个end (注:关键字,if,e ...
- [vijos P1880]ファーラの力
据说这是一道 JOI 的题?反正我觉着挺好的喵~ 题目看起来十分可怕,但是代码还是很短的 显而易见的,ans 因分为3个部分:1.中途增加光压的时间 2.中途减少光压的时间 3. 所有路程的总时间 发 ...
- aptitude解决Ubuntu各种依赖问题
转自:http://allog.ml/linux/aptitude%E8%A7%A3%E5%86%B3ubuntu%E5%90%84%E7%A7%8D%E4%BE%9D%E8%B5%96%E9%97% ...