Reputation: 3387
As strong and weak properties are new in iOS 5. If any one tell me which property is used when.
When we should use strong or when we should use weak?
@property(nonatomic,strong)
@property(nonatomic,weak)
Upvotes: 4
Views: 9939
Reputation: 89
There are following difference between strong and weak.
1.If we declare variable strong then it is not deallocated by compiler till the Application instance in memory.When we set nil value to that reference then it deallocates by compiler, by default any local variable as strong variable. For instance:- var str = "hello world"
if we set str = nil then it is deallocated.
2.If we declare variable as strong then it is retain by other instance(Class) and it's retain count increment by 1.
Weak property.
1.When we declare weak property then it only contains data/instance address till strong reference is in memory if strong variable reference deallocated it is automatically set to nil.
For ex:- var str = "hello world" weak var stringVar = str
suppose str contain 200 heap address and we set str = nil, then automatically weak property reference set to nil by the compiler.
So that's the reason in stoary board ref controller, Main view only set to strong and other are weak for ex- we can see UIButton,UILabel out let e.t.c.
Upvotes: 0
Reputation: 5926
Review Apple documentation for the Automatic Reference Counting (ARC)
If you don't have time for reading it:
ARC introduces several new lifetime qualifiers for objects, and weak references. A weak reference does not extend the lifetime of the object it points to, and automatically becomes nil when there are no strong references to the object.
strong is the default. An object remains “alive” as long as there is a strong pointer to it.
weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
As iOS 5 ARC automatically nullifies weak links, when an object is unloaded its object hierarchy is automatically set to nil. Because this reason, Weak
is the recommended relationship for all outlet
properties. These view objects are already part of the view controller’s view hierarchy and don’t need to be retained elsewhere. The big advantage of declaring your outlets weak is that it saves you time writing the viewDidUnload method.
Check a very detailed document refering memory management. It is previous to ARC, but it will help you to understand the memory management. The retain keyword for properties still works with ARC and is simply a synonym for strong. Or another specific ARC tutorial.
Upvotes: 3
Reputation: 19418
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
.
Weak
instead, means that the object the property points to, is free to release but only if it sets the property to nil
. In ARC you use weak to ensure you do not own the object it points to.
Upvotes: 2
Reputation: 15376
strong
is like retain
, weak
is like assign
. The main difference is that weak
properties turn to nil
when the object that is assigned to them gets released.
eg:
@property (nonatomic, weak) id test;
...
- (void)example
{
id foo = [[NSObject alloc] init];
self.test = foo;
foo = [[NSObject alloc] init];
assert(self.test == nil);
}
Upvotes: 2