Strong and weak are keywords that help you manage the Automatic Reference Counting (ARC) of the XCode environment and are a part of the new way of managing memory in Objective-C. To understand the difference, you must first understand how memory is implicitly managed by objects in XCode.
Each object has a property that tracks the number of other objects that require/reference it. This property is called the reference count. In the old way of doing things, you would specifically tell the object to be retained (increase its reference count). When you did this, you would also have to explicitly tell the object to decrease its reference count (release) when you were done with it.
When an object’s reference count reaches zero, it is destroyed and its memory return to the heap where it can be reused. If you forgot to retain the object, it could potentially be destroyed earlier than you intended and cause an error in your program—there are reasons why you wouldn’t retain an object. If you forgot to release the object, or you write code in such a way that it could never be released then it could reside in memory even after you were done with it, causing a memory leak that could slow your app down or even crash it after sometimes.
Now, with ARC you no longer have to explicitly tell an object to be retained or released. Pointers are automatically created with strong references; they are automatically retained. In this case, the strong keyword is implied when creating pointers, though you can use it if you want.
NSString *nameString = self.nametextField.text;
the nameString variable becomes a pointer to the NSString object that holds the contents of text field, thenameString variable is now the owner of that string object.
An object can have more than one owner. Until the user changes the contents of the UITextField, the text property of UITextField is also an owner of the string object. There are two pointers keeping that same string object alive:
Moments later the user will type something new into the text field and its text property now points at a new string object. But the original string object still has an owner (the nameString variable) and therefore stays in memory.
Only when nameString gets a new value too, or goes out of scope — because it’s a local variable, or because it’s an instance variable and the object it belongs to is deallocated — does the ownership expire. The string object no longer has any owners, its retain count drops to 0 and the object is deallocated.
We call pointers such as nameString and textField.text “strong” because they keep objects alive. By default all instance variables and local variables are "strong pointers".
However there are times when you don’t want to retain an object (i.e. increase its reference count). To do this, you use the weak keyword when creating the pointer. Your pointer will still point to the object, but if that object is destroyed then using the pointer will cause a program error.
Why would you use a weak reference?
Well, one reason is if you have objects in parent-child relationships where the parent keeps pointers to its children and every child keeps a pointer to its parent. The potential with this structure is that you could no longer need your parent object and XCode will implicitly release it, but because its children keep a reference to it, the parent and its children can remain in memory even though you no longer have a way of accessing them. This is called a retain cycle.
To avoid the retain cycle, the children objects should only maintain a weak pointer to their parents. The reason for this is that when the parent is told to destroy itself, the children would be destroyed first so there is no chance they could access their parent after it had been destroyed.
Here’s a link to apple doc that explains it much better:
Just because ARC takes care of doing retain and release for you in the proper places, doesn’t mean you can completely forget about memory management altogether. Because strong pointers keep objects alive, there are still situations where you will need to set these pointers to nil by hand, or your app might run out of available memory.
If you keep holding on to all the objects you’ve ever created, then ARC will never be able to release them. Therefore, whenever you create a new object, you still need to think about who owns it and how long the object should stay in existence.There is no doubt about it, ARC is the future of Objective-C.
Apple encourages developers to turn their backs on manual memory management and to start writing their new apps using ARC. It makes for simpler source code and more robust apps. With ARC, the developers are assured that the memory related crashes will no longer be their head ache.
Note: You may often come across code that isn’t compatible with ARC yet, whether it’s your own code or third-party libraries. But you may convert it to ARC I will show that in my next post.
Hope you have understood the concept of strong and weak pointer system, if you have any queries kindly mail me or ask them via comments.
Happy iCoding and have a great day.