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. DTO概念

    在开发过程中用到了DTO,简单了解了一下. DTO:数据传输对象,用来连接表现层和应用层之间的数据交互.数据传输对象是没有行为的POJO对象,它的目的只是为了对领域对象进行数据封装,实现层与层之间的数 ...

  2. wp8.1 Study19:通知

    一.通知形式 在windowsphone系统中,通知有很多中形式,如下图 (Tile磁贴在前一博文已复习过,Badge形式与tile类似) 1.Toast 它主要是利用xml文件来编写的,xml代码如 ...

  3. Java - 简单的对象排序 - Comparator

    注:对象排序,就是对对象中的某一字段进行比较,以正序或倒序进行排序. 例: 需要排序的对象: public class Person { public int age; public String n ...

  4. Linux驱动之HelloWorld

    最近看android的一些源码,里面有一些功能是用驱动实现的.于是就兴起看了一些驱动相关的东西,准备日后深入.这没有技术含量的水文,仅作为日后的备忘吧. 系统使用的是ubuntu 12.0.04,内核 ...

  5. Asp.net MVC 视图(三)

    Html辅助方法(HtmlHelper类) 可以通过视图的Html属性调用HTML辅助方法,也可以通过Url属性调用URL辅助方法,还可以通过Ajax属性调用Ajax辅助方法. 在控制器中也存在Url ...

  6. UltraISO制作大于4G文件的光盘映像可启动U盘

    1.使用常规方法 制作 u盘启动 启动-->写入硬盘映像-->写入 2.制作成功后U盘 是FAT32格式 对于FAT32文件系统,其缺点不能存储超过4G的文件,而对于NTFS文件系统,则没 ...

  7. MJExtension框架介绍

    MJExtension框架介绍 标签: MJExtension 2015-05-01 08:22 1120人阅读 评论(0) 收藏 举报  分类: Foundation(14)  版权声明:本文为博主 ...

  8. Jquery EasyUI DataGrid .net实例

    前台界面:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  9. HttpClient(4.3.3)实例讲解

    HttpClient的作用强大,真的是十分强大. 本实例是基于v4.3.3写的,,作用是模拟登陆后进行上下班打卡,,,使用htmlparser进行解析返回的html文件 关于HttpClient的一些 ...

  10. js 轮播图代码

    js代码 (function(){ /** parent //父容器 changeTime //每次间隔几秒切换下一条 leaveTime //鼠标从小图上离开过后几秒继续切换 index //从第几 ...