Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 722
Dinesh Avatar asked Jun 23 '12 11:06

Dinesh


2 Answers

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.

like image 115
Phillip Avatar answered Nov 15 '22 07:11

Phillip


In ARC (Automatic Reference Counting) Strong tells the compiler that the property-owner relationship is "strong". It is equivalent to retain in the autorelease pool memory scheme. Apple has a article on transitioning to ARC here.

like image 45
doggiedoc Avatar answered Nov 15 '22 08:11

doggiedoc