Strong and Weak in iOS

I had to refer to this in my blog, because its one of those examples out there which makes it really easy to understand the difference between strong and weak pointers.
First of, I will copy the apple definitions and then paste the example, see the difference.

  • __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.

Now the example ( I got this example from stackoverflow (always give credit where its due) http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5 ) –
The difference is that an object will be deallocated as soon as there are no strong pointers to it. Even if weak pointers point to it, once the last strong pointer is gone, the object will be deallocated, and all remaining weak pointers will be zeroed out.
Imagine our object is a dog, and that the dog wants to run away (be deallocated).
Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.
Weak pointers, on the other hand, are like little kids pointing at the dog and saying “Look! A dog!” As long as the dog is still on the leash, the little kids can still see the dog, and they’ll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.
As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.


To summarize, the new modifiers for properties are:

  • strong. This is a synonym for the old “retain”. A strong property becomes an owner of the object it points to.
  • weak. This is a property that represents a weak pointer. It will automatically be set to nil when the pointed-to object is destroyed. Remember, use this for outlets.
  • unsafe_unretained. This is a synonym for the old “assign”. You use it only in exceptional situations and when you want to target iOS 4. More about this later.
  • copy. This is still the same as before. It makes a copy of the object and creates a strong relationship.
  • assign. You’re no longer supposed to use this for objects, but you still use it for primitive values such as BOOL, int, and float.

 

// The following declaration is a synonym for: @property(retain) MyClass *myObject;
@property(strong) MyClass *myObject;
// The following declaration is similar to "@property(assign) MyClass *myObject;"
// except that if the MyClass instance is deallocated,
// the property value is set to nil instead of remaining as a dangling pointer.
@property(weak) MyClass *myObject;
***************************************************************************************************************
Another example showing weak property from wenderlich website:
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
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.
Currently our viewDidUnload looks like this:
- (void)viewDidUnload
{
	[super viewDidUnload];
	self.tableView = nil;
	self.searchBar = nil;
	soundEffect = nil;
}
You can now simplify it to the following:
- (void)viewDidUnload
{
	[super viewDidUnload];
	soundEffect = nil;
}

Leave a Reply

Your email address will not be published. Required fields are marked *