1

Hi every one whether some one will elaborate me that why it is recommended to use the _weak reference for delegates and not the strong reference? though we can also use the strong reference for delegates. Some one will please tell me with the better and descriptive example with simple way that in which situations should we use the strong reference and in which situations should we use the _weak reference for the delegates.

I went through one of the related question on stack overflow

Is it ever Ok to have a 'strong' reference for a delegate?

but it did not clear my concept properly.

Any help will be highly appreciated !!

Thanks.

Community
  • 1
  • 1
iShwar
  • 1,625
  • 1
  • 16
  • 31

2 Answers2

3

Using __strong on delegate is very easy to create retain cycle:

say A has a strong reference to B, and some object set A as a delegate of B, if the delegate is strongly referenced, then, a retain cycle is formed.

CarmeloS
  • 7,868
  • 8
  • 56
  • 103
  • will you please tell me some examples that where we prefer to use _weak and where we prefer to use strong . – iShwar Jun 21 '14 at 12:46
  • 1
    In my opinion, you should never use a strong reference in a delegate pattern, if you do need to keep your delegate alive, you should using some object outside the delegate relationship to do this. Always using weak in delegate pattern make your code easy to debug and consistent. – CarmeloS Jun 23 '14 at 02:03
0

Yes, It's east to creat a retain cycle while using __strong on delegate.

Addtionally, in ARC, we using weak on delegate,while the object using the delegate is released,the delegate will be nil automatically.

Dennis
  • 455
  • 2
  • 7
  • 14
  • https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html This can explain your issue verywell – Dennis Jun 25 '14 at 03:21