I have switched my project to ARC, and I don't understand if I have to use strong
or weak
for IBOutlets. Xcode do this: in interface builder, if a create a UILabel
for example and I connect it with assistant editor to my ViewController
, it create this:
@property (nonatomic, strong) UILabel *aLabel;
It uses the strong
, instead I read a tutorial on RayWenderlich website that say this:
But for these two particular properties I have other plans. Instead of
strong
, we will declare them asweak
.
@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 outletsweak
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;
}
So use weak
, instead of the strong
, and remove the set to nil in the videDidUnload
, instead Xcode use the strong
, and use the self... = nil
in the viewDidUnload
.
My question is: when do I have to use strong
, and when weak
?
I want also use for deployment target iOS 4, so when do I have to use the unsafe_unretain
? Anyone can help to explain me well with a small tutorial, when use strong
, weak
and unsafe_unretain
with ARC?