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的更多相关文章

  1. 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】

    @property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...

  2. ARC声明属性关键字详解(strong,weak,unsafe_unretained,copy)

    ARC声明属性关键字详解(strong,weak,unsafe_unretained,copy) 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都很熟悉了, ...

  3. 对于atomic nonatomic assign retain copy strong weak的简单理解

    atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环 ...

  4. IOS—— strong weak retain assign 学习

    转自:http://wenzongliang.iteye.com/blog/1746604 简单讲strong等同retain weak比assign多了一个功能,当对象消失后自动把指针变成nil,好 ...

  5. @property中的copy.strong.weak总结

    1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...

  6. assign,copy,strong,weak,nonatomic的理解

    举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一段 ...

  7. property attribute: assign, strong, weak, unsafe_unretain and copy

    assign:用于“纯量类型”(如CGFloat 或 NSInteger等): strong:用于“对象类型”,定义了一种“拥有关系”(owning relationship),为这种属性设置新值时, ...

  8. ios copy/strong/weak..使用总结

    总结 关于属性的这些选项的学习,做一下总结: 所有的属性,都尽可能使用nonatomic,以提高效率,除非真的有必要考虑线程安全. NSString:通常都使用copy,以得到新的内存分配,而不只是原 ...

  9. [iOS基础控件 - 6.12.3] @property属性 strong weak copy

    A.概念 @property 的修饰词   strong: 强指针/强引用(iOS6及之前是retain) weak: 弱智真/弱引用(iOS6及之前是assign)   默认情况所有指针都是强指针 ...

随机推荐

  1. jdk6提供的加密算法

    SUN:SHA1PRNG____sun.security.provider.SecureRandomSUN:SHA1withDSA____sun.security.provider.DSA$SHA1w ...

  2. 在 Vultr VPS 中 以 Debian 8 i386 (jessie) 为 操作系统 平台 手动 搭建 PPTP VPN 全过程

    更新服务器并安装 PPTP 服务  apt-get update apt-get upgrade apt-get install pptpd 编辑 /etc/pptpd.conf 找到 #locali ...

  3. nslog一些用法

    1.nslog打印方法出来 NSLog(@"%@",NSStringFromSelector(_cmd)); 2.debug模式下打印一些信息,release模式下则不打印 #if ...

  4. 电脑无法登陆ftp

    电脑无法登陆ftp,或者对于少数ftp能登陆,大多数不能登陆,用了一大堆ftp软件一样登陆不了.后来baidu了一下,发现是防火墙的问题.据说是Windows防火墙阻止了20/21端口的通信,说白了就 ...

  5. UIMenuController的使用,对UILabel拷贝以及定制菜单

    分类: ios开发2012-08-06 17:15 11961人阅读 评论(0) 收藏 举报 actionmenuuiview 1. Menu所处的View必须实现 – (BOOL)canBecome ...

  6. solr异常解决

    使用solr1.4跟solr3.1时,配置了个唯一id类型是sint 或者int,启动时在tomcat后台就会抛出这个异常: org.apache.solr.common.SolrException: ...

  7. Android FM 模块学习之四 源码解析(1)

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 前一章我们了解了FM手动调频,接下 ...

  8. 【Avalon源码】iterator

    function iterator(vars, body, ret) { var fun = 'for(var ' + vars + 'i=0,n = this.length; i < n; i ...

  9. Qt资源下载、安装、配置

    (一)资源下载: 硕士毕业论文要做一个仿真平台,在linux环境下利用Qt开发. 自己有一定的c/c++基础,Qt是零基础接触.所以,经过一番查找,发现youtube一个外国友人Bryan从零开始教Q ...

  10. 如何成为一名优秀的前端工程师 (share)

    发现一篇不错的博文,和大家分享一下,为有志成为一名优秀前端工程师的童鞋们提供一个参考. :)~ 本文来源:http://www.biaodianfu.com/what-makes-a-good-fro ...