Objective-C ARC: strong vs retain and weak vs assign Ask Question

Objective-C ARC: strong vs retain and weak vs assign Ask Question

There are two new memory management attributes for properties introduced by ARC, strong and weak.

Apart from copy, which is obviously something completely different, are there any differences between strong vs retain and weak vs assign?

From my understanding, the only difference here is that weak will assign nil to the pointer, while assign won't, which means the program will crash when I send a message to the pointer once it's been released. But if I use weak, this won't ever happen, because message send to nil won't do anything.

I don't know about any differences between strong and retain.

Is there any reason why should I use assign and retain in new projects, or are the kind of being deprecated?

ベストアンサー1

After reading so many articles Stackoverflow posts and demo applications to check variable property attributes, I decided to put all the attributes information together:

  1. atomic //default
  2. nonatomic
  3. strong=retain //default
  4. weak
  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).

ストロング ポインターは、犬に付けたリードのようなものです。リードを犬に付けている限り、犬は逃げません。5 人が 1 匹の犬にリードを付けると (5 匹のストロング ポインターが 1 つの物体に付けた場合)、5 本のリードがすべて外されるまで犬は逃げません。

一方、弱い指示犬は、小さな子供が犬を指差して「見て!犬だ!」と言うようなものです。犬がまだリードにつながれている限り、小さな子供は犬を見ることができるので、犬を指差します。しかし、すべてのリードが外されると、どんなに多くの子供が犬を指差しても、犬は逃げてしまいます。

最後の強いポインタ (リーシュ) がオブジェクトを指していなくなるとすぐに、オブジェクトは割り当て解除され、すべての弱いポインタはゼロになります。

いつ弱いを使うのですか?

弱いオプションを使用する必要があるのは、保持サイクルを回避したい場合のみです (たとえば、親が子を保持し、子が親を保持するため、どちらも解放されません)。

3.保持 = 強い

  • 保持され、古い値は解放され、割り当てられます。保持は新しい値が送信されることを指定します。
  • 割り当て時に保持し、古い値を送信する -release
  • 保持は強力と同じです。
  • Apple によれば、retain と記述すると自動的に変換され、strong のみのように動作するとのことです。
  • 「alloc」のようなメソッドには暗黙の「retain」が含まれる

例:

@property (nonatomic, retain) NSString *name;

@synthesize name;

4.割り当てる

  • 割り当てはデフォルトで、単に変数の割り当てを実行します。
  • 割り当ては、プロパティのセッター実装をどのように合成するかをコンパイラに指示するプロパティ属性です。
  • C プリミティブ プロパティには assignment を使用し、Objective-C オブジェクトへの弱参照には weakness を使用します。

例:

@property (nonatomic, assign) NSString *address;

@synthesize address;

おすすめ記事