<分区>
我已经将我的项目切换到 ARC,我不知道我是否必须为 IBOutlets 使用 strong
或 weak
。 Xcode 这样做:在界面生成器中,如果创建一个 UILabel
,然后我将它与助理编辑器连接到我的 ViewController
,它会创建这个:
@property (nonatomic, strong) UILabel *aLabel;
它使用 strong
,我在 RayWenderlich 网站上阅读了这样的教程:
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;
}
所以使用weak
,而不是strong
,并在videDidUnload
中移除设置为nil,Xcode使用 strong
,并在 viewDidUnload
中使用 self... = nil
。
我的问题是:什么时候必须使用strong
,什么时候使用weak
?
我还想用于部署目标 iOS 4,那么什么时候必须使用 unsafe_unretain
?当使用 strong
、weak
和 unsafe_unretain
与 ARC 时,任何人都可以通过一个小教程帮助我很好地解释?