Search code examples
iphoneiosxcodeios5

Difference between Strong and Weak IBOutlets


What is the difference between strong and weak IBOutlets in the Xcode iOS 5.1 SDK?

I was previously using the 4.3 SDK, where strong IBOutlets did not exist. In addition, (auto)release is not available in the iOS 5.1 SDK.


Solution

  • Strong means that as long as this property points to an object, that object will not be automatically released. In non-ARC it's a synonym for retain

    Specifies that there is a strong (owning) relationship to the destination object.

    Weak instead, means that the object the property points to, is free to release but only if it sets the property to NULL. In ARC you use weak to ensure you do not own the object it points to

    Specifies that there is a weak (non-owning) relationship to the destination object. If the destination object is deallocated, the property value is automatically set to nil.

    Nonatomic means that if multiple threads try to read or to change the property at once, badness can happen. Consequences are that there will be partially-written values or over-released objects = CRASH.

    Take also a look here, at Apple's documents.

    From there, examples are

    @property (weak) IBOutlet MyView *viewContainerSubview;
    @property (strong) IBOutlet MyOtherClass *topLevelObject;
    

    Check also this to know more about strong and weak.