__weak and __unsafe_unretained are the ownership qualifiers introduced by LLVM Compiler 3.0.
By default all pointers are __strong under ARC which means that when an object is assigned to a pointer, it is retained for as long as that pointer refers to it. This causes the retain cycles.
Lets say you have two objects one contains another as an instance variable and second object has strong link to first one. then this will cause retain cycle and both objects will never be released.
Their most common use can be find while using delegate where one defines the property of a delegate with "__weak" or "__unsafe_unretained". This means that the delegate instance variable will still point back at the first object, but it will not cause that object to be retained, thus breaking the retain cycle and allowing both objects to be released.
We can declare any object 'weak' or 'unsafe unretained' by using keyword '__weak' and '__unsafe_unretained'. These are very useful to break any retain cycle. the above release can be done using __weak or __unsafe_unretained.
Note* '__weak' is only supported for iOS 5.0 and Lion as deployment targets.
If you want to target back to iOS 4.0 and Snow Leopard, you have to use the '__unsafe_unretained'.
0 Comment(s)