2

Can anyone provide me link or information about :

why weak variable is beneficial as compared to strong variable?

and IBOutlets in ARC, that should be weak or strong? Why delegate is also defined as weak property?

Thanks in advance.

Lotus Shah
  • 493
  • 4
  • 13

3 Answers3

0

The weak like the world is weak retain to object, when it dealloc that will be nil, and we can avoid the retain cycle. IBOutlets property because the view had retain it so that needn't strong retain it again.

0

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.

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW3

Akshay Degada
  • 139
  • 2
  • 13
-2

The current recommended best practice from Apple is for IBOutlets to be strong unless weak is specifically needed to avoid a retain cycle.

In general you should make your outlet strong, especially if you are connecting an outlet to a subview or to a constraint that's not always going to be retained by the view hierarchy. The only time you really need to make an outlet weak is if you have a custom view that references something back up the view hierarchy and in general that's not recommended.

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
  • Do you have a pointer to Apple's current recommendation? When you use IB to create the outlet, I believe it defines it as weak. – Avi Jun 13 '16 at 11:34
  • IBOutlet should be strong, for performance reason...https://www.invasivecode.com/weblog/storyboard-strong-iboutlet-scene-dock/?doing_wp_cron=1465817938.3935289382934570312500 – Suraj Sukale Jun 13 '16 at 11:41
  • 3
    That blog post is wrong. The documentation it links to states clearly that declared properties should be `weak`. The blog writer is promoting a premature optimization (avoiding the clearing of weak references), and is a poor programmer. Don't listen to him. – Avi Jun 13 '16 at 11:57
  • OK.. thanks alot @Avi.. for giving me such a golden information...thanks..! – Suraj Sukale Jun 13 '16 at 12:00