0

I'm still struggling with the whole weak and strong part in iOS: I have now my app almost finished and actually all objects I have in my view controllers are weak and the app is working. But is this correct? So far what I've learned Outlets should be always weak and they are. But what about objects for the model of a view controller? Should they also be weak or strong?

Or in other words: When do I have to use strong?

MichiZH
  • 4,985
  • 11
  • 37
  • 78
  • Here is a nice explanation [http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5](http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5) – Asike Dec 28 '13 at 09:01
  • you can think of it in terms of ownership... objects that your view controller owns or are essential to it's operation should be strong. references that are owned by other objects (for example your objects can be owned by your NIB can be weak) – nielsbot Dec 28 '13 at 09:08
  • 1
    another case: if you have a parentchild relationship for example superview/subviews... the parent owns it's children (subviews is strong) whereas the children don't own their parents (superview is weak). stick to this rule to avoid retain cycles. – nielsbot Dec 28 '13 at 09:10
  • it highly depends on your concept of memory management; it defines which one of the `weak` and `strong` is good choice. – holex Dec 28 '13 at 12:24

1 Answers1

2

You should understand about memory management. Read this topic Manage the Object Graph through Ownership and Responsibility.

I suggest with use of weak and strong.

strong -You can make strong property for below list

  • model object
  • yourdatasource property
  • Programmtically created UIobject(UIButton,UILabel.. etc)

weak: - You can make weak property for below list

  • delegate must be weak property

  • All IBOutlet must be weak property

Notes: Any visitors may edit my answer with your view. I know, I have missed lots of element in those list.

Community
  • 1
  • 1
Mani
  • 17,226
  • 13
  • 73
  • 97
  • So as a general rule (I like it simple :-): All objects I create in my .h file I make strong EXCEPT all IBOutlets created by drag and drop in interface builder correct? – MichiZH Dec 28 '13 at 09:27
  • 1
    Ya correct. But don't keep all object in .h file. If you use object for current view controller, it must visible to current view controller only. If you create as public property, it will visible to other viewcontroller also. It is correct but it is like baby programming. read this Encapsulating Data - https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html – Mani Dec 28 '13 at 09:30