Differences between strong and weak in Objective-C

后端 未结 9 1254
礼貌的吻别
礼貌的吻别 2020-11-22 08:09

I\'m new to Obj-C, so my first question is:

What are the differences between strong and weak in @property declarations of poin

相关标签:
9条回答
  • 2020-11-22 08:30

    strong is the default. An object remains “alive” as long as there is a strong pointer to it.

    weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.

    0 讨论(0)
  • 2020-11-22 08:31

    A dummy answer :-

    I think explanation is given in above answer, so i am just gonna tell you where to use STRONG and where to use WEAK :

    Use of Weak :- 1. Delegates 2. Outlets 3. Subviews 4. Controls, etc.

    Use of Strong :- Remaining everywhere which is not included in WEAK.

    0 讨论(0)
  • 2020-11-22 08:32

    strong: assigns the incoming value to it, it will retain the incoming value and release the existing value of the instance variable

    weak: will assign the incoming value to it without retaining it.

    So the basic difference is the retaining of the new variable. Generaly you want to retain it but there are situations where you don't want to have it otherwise you will get a retain cycle and can not free the memory the objects. Eg. obj1 retains obj2 and obj2 retains obj1. To solve this kind of situation you use weak references.

    0 讨论(0)
提交回复
热议问题