[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;
 




ARC 下属性 strong,weak,还有copy 等属性详解

http://www.jianshu.com/p/4a1d1921284b 虽然的确是最基本&被无数人写过的问题,但是今天还是想弄得更清楚一些,所以看了看官方文档,写了这篇博客。 assign...
  • jeffasd
  • jeffasd
  • 2016年06月18日 16:54
  • 1034

探讨声明UI控件属性 什么时候用weak什么时候用strong

也许我们在学习的时候,有人会告诉我们,UI控件的属性用weak修饰,而且,从Storyboard中拖出来的控件也都是用weak来修饰的,所以在习惯上,我们总会用weak去修饰。然而,在某些情况下,这么...
  • qinqi376990311
  • qinqi376990311
  • 2016年10月26日 14:33
  • 1562

iOS基础:strong属性与weak属性的区别

一、简介 ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain、release、autorelease语句。你不再需要担心内存管理,因为...
  • gongwutianya
  • gongwutianya
  • 2016年04月01日 08:26
  • 2788

iOS中代码创建UIView的时候,使用weak还是strong?

我们知道,从 Storyboard 往编译器拖出来的 UI 控件的属性是 weak 的,那么代码创建uiview的时候呢? 我认为 UI 控件用默认用 weak,根源还是苹果希望只有这些 UI 控...
  • Cjlcooper
  • Cjlcooper
  • 2016年12月08日 22:59
  • 714

UI控件用weak还是用strong修饰

#import "ViewController.h" @interface ViewController () @property (nonatomic, weak) UIView *weakVie...
  • u011010305
  • u011010305
  • 2016年09月13日 09:28
  • 1366

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

作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/17694129 转载请注明出处 如果觉得文章对你有所帮...
  • u012588310
  • u012588310
  • 2014年09月11日 17:57
  • 1323

Swift 引用计数总结 Strong,Weak, unowned 简单使用

每天一小结,必须读几篇博客在闲暇时,下面开始进入正题: ARC ARC 苹果版本的自动内存管理的编译时间特性。它代表了自动引用计数(Automatic Reference Counting...
  • LiChang719
  • LiChang719
  • 2016年02月02日 16:17
  • 4504

delegate 为什么应该是 weak 类型而不是strong类型

delegate 为什么应该是 weak 类型而不是strong类型 循环引用 对象a创建并引用了对象b.对象b创建并引用了对象c.对象c创建并引用了对象b. 这时候b和c的引用计数分别...
  • fxmmc
  • fxmmc
  • 2014年10月16日 18:59
  • 1563

iOS开发笔记之五十七——__weak与__strong是如何解决循环引用的

当我们写一个block时,如果你的block涉及被self持有以及需要访问self的成员时,循环引用问题由此产生。解决的办法也很简单,其中利用__weak与__strong是常见的手段,类似代码如下:...
  • lizitao
  • lizitao
  • 2017年02月03日 13:55
  • 1534

关于声明控件用weak和strong的问题&NSString用copy好还是strong

1、控件声明strong好还是weak? 相信很多人都被这个问题困惑过,我也不例外,所以做下总结,希望能帮到大家,有不对的欢迎拍砖! 以创建一个lable为例,在storyboard里用拖线的方式...
  • hah1992
  • hah1992
  • 2015年11月22日 11:23
  • 836
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Objective-C] strong/weak 属性——狗项圈的比喻
举报原因:
原因补充:

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