0

I pass an object from a xviewController to yviewController via segue, The object obj is initalized at xviewController as a strong property.

I pass the object to yviewController's property which is a pointer of the same class. When a viewcontroller dissappears, is it still pointing that address or does it just sets it to nil. The question is, should i define this second pointer as a weak pointer or a strong ?

ilker
  • 67
  • 1
  • 10

2 Answers2

0

If I understand you correctly then it should be strong (the default). When the second view controller is destroyed ARC will assign the object pointer to nil removing one reference, however you will still want to reference the object in the first view controller.

Droppy
  • 9,691
  • 1
  • 20
  • 27
0

The correct answer is it depends.

First you have to understand the difference between strong and weak.

Making it short, a strong reference owns the object you are referencing with this property. What means that the compiler will make sure that this object will not be removed from memory while there is a strong reference to it. If you have two objects pointing strongly to another, and one is removed, the second one will still have this object, because it owns it.

A weak reference basically means that the objected is not owned, it's ownership is still from who has the strong reference, so in case the strong reference stop existing, the weak will be then pointing to nil. So repeating, A has a strong reference to X, B has a weak reference to X, if A removes this reference, B will lose it too..

So.. back to your question, weak or strong in the second View controller, it will depend.. is there a possibility that the first view controller will destroy the object while the second view controller is visible? If this happen you want to keep your copy of the object? If booth yes, put it strong.

If there is no way that the first reference is destroyed, or you want to reflect updates in the first object (updates keeping the instance) in the second view controller, a weak reference is enough.

Roberto Ferraz
  • 2,511
  • 24
  • 43
  • i got the idea but i can't be sure if my first view's controller would still strongly pointing that object when i perform a segue.. can you please look at my storyboard http://s22.postimg.org/8uime6r69/Screen_Shot_2014_08_07_at_11_01_44.gif I actually feel like i should be using a class which is declared in a shared memory area sth. Because i'll be using the same model in 3 more controllers (i hope it's a legal usage in MVC). I made some research and found the Singletons. Would it be a wisely way to that, than passing the objects everytime when i switch my view. – ilker Aug 07 '14 at 09:17
  • Looking at your storyboard that first viewController will only lose the reference if you set the property to nil. That first view controller won't be deallocated, so, it won't lose the reference. About the singleton, depends again of the whole concept of your app.. I could explain it here.. but I recommend you to read this: http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton – Roberto Ferraz Aug 07 '14 at 15:19
  • I pressed enter by accident, and can't add much more lines during and edit.. so to finish.. One simple part of the answer from that question I sent you.. is: Simple things to help you decide about singleton: 1 - It controls concurrent access to a shared resource. 2 - access to the resource will be requested from multiple, disparate parts of the system. 3 - there can be only one object (not different versions of it) – Roberto Ferraz Aug 07 '14 at 15:22