[Objective-C] strong/weak 属性——狗项圈的比喻

转载 2015年07月10日 09:05:11

转自:

http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign

http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html


BJ Homer 对 strong/weak reference 给出了非常形象的比喻,很到位,摘抄如下:


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.


对于 strong/weak/retain/assign 的解释,摘抄如下:


  1. atomic //default
  2. nonatomic
  3. strong = retain
  4. weak = assign
  5. retain
  6. assign //default
  7. unsafe_unretained
  8. copy
  9. readonly
  10. readwrite //default

Below is the detailed article link where you can find above mentioned all attributes, that will definitely help you. Many thanks to all the people who give best answers here!!

Variable property attributes or Modifiers in iOS

1.strong (iOS4 = retain )

  • it says "keep this in the heap until I don't point to it anymore"
  • in other words " I'am the owner, you cannot dealloc this before aim fine with that same as retain"
  • You use strong only if you need to retain the object.
  • By default all instance variables and local variables are strong pointers.
  • We generally use strong for UIViewControllers (UI item's parents)
  • strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it.Using the keyword strong means that you own the object.

Example:

@property (strong, nonatomic) ViewController *viewController;

@synthesize viewController;

2.weak -

  • it says "keep this as long as someone else points to it strongly"
  • the same thing as assign, no retain or release
  • A "weak" reference is a reference that you do not retain.
  • We generally use weak for IBOutlets (UIViewController's Childs).This works because the child object only needs to exist as long as the parent object does.
  • a weak reference is a reference that does not protect the referenced object from collection by a garbage collector.
  • Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil

Example :

@property (weak, nonatomic) IBOutlet UIButton *myButton;

@synthesize myButton;

Strong & Weak Explanation, Thanks to BJ Homer:

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).

3.retain = strong

  • it is retained, old value is released and it is assigned retain specifies the new value should be sent
  • retain on assignment and the old value sent -release
  • retain is the same as strong.
  • apple says if you write retain it will auto converted/work like strong only.
  • methods like "alloc" include an implicit "retain"

Example:

@property (nonatomic, retain) NSString *name;

@synthesize name;

4.assign

  • assign is the default and simply performs a variable assignment
  • assign is a property attribute that tells the compiler how to synthesize the property's setter implementation
  • I would use assign for C primitive properties and weak for weak references to Objective-C objects.

Example:

@property (nonatomic, assign) NSString *address;

@synthesize address;
 




相关文章推荐

Objective-C学习——copy, retain,assign,readonly,readwrite,strong,weak,nonatomic,unsafe_unretained

说明:看了很多书上的描述,对retain assign和copy的都是草草描述,没有谈及本质,但是retain assign和copy却是内存管理的重要内容,所以自己总结了下,本文涉及到了Object...
  • yi412
  • yi412
  • 2016-05-20 11:31
  • 430

Objective-c - @property的参数strong与weak

/*    Objective-c - @property的参数strong与weak       1,如果使用strong来修饰类的属性,代表@property生成的私有属性...

Objective-C中copy 、retain以及ARC中新加入的strong、weak关键字的含义

copy: 创建一个引用计数为1的对象,然后释放旧的对象 retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的引用计数为 1 Copy其实是建立了一个相同的对象,而retain不...

浅析objective-c中的strong和weak

在才开始学习oc时,搞不懂什么时候用strong,什么时候用weak,经过一段时间的学习,我谈谈我对strong和weak的理解。 首先strong和weak这两个关键字是用来修饰变量,表示这个变量是...

《Objective-C高级编程:引用计数和strong ,weak

作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/17694129 转载请注明出处 如果觉得文章对你有所帮...

Objective-c 的 内存管理和@property strong,weak,assign,copy,retain等关键字介绍

@property strong,weak,assign,copy,retain等关键字@property介绍 OC意识到当getter方法和setter方法遇到一个类有大量的实例变量时,ge...

Objective-C语言特性——强引用、弱引用(Strong、Weak)

转自:独自一人 » Objective-C语言特性——强引用、弱引用(Strong、Weak) 编写Objective-C代码的很大一部分工作是管理可执行代码保持的对象引用,还有被引用对象保持的对另...

Objective-C属性修饰符strong和copy的区别

记得刚参加工作的时候总是对OC中的属性修饰符strong和copy弄不明白,也总是被这两个东西坑,慢慢工作时间长了才真正了解了它,不过今天在项目中又被这两个东西给坑了一次,索性呢今天就把它记录下来,也...

IOS-36-Object-C语法之属性关键字的使用(assign/weak/strong/copy)

一、 assign 用于 ‘基本数据类型’、‘枚举’、‘结构体’ 等非OC对象类型 eg:int、bool等 二、 weak 一般应用: UI控件 详细说明: 为什么建...
  • IOT_LI
  • IOT_LI
  • 2015-12-02 16:07
  • 1382

iOS OC内存管理、ARC、property属性、__strong、__weak、__block——iOS 编码复习(一)

首先来聊聊内存管理。因为是先有了内存管理这个东西,才慢慢的有了ARC,而后才会有@property的各种属性 聊到内存管理,我们就能知道iOS5之前,iOS的内存管理是MRC(手动内存管理)的。iOS...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)