IBOutlet和其他弱者或弱者 [英] weak or strong for IBOutlet and other
问题描述
我已将项目切换为ARC,我不明白是否必须使用 strong
或弱
对于IBOutlets。 Xcode执行此操作:在界面构建器中,如果创建一个 UILabel
,我将它与助理编辑器连接到我的 ViewController
,它创建了这个:
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;
它使用 strong
,而不是我读过RayWenderlich网站上的一个教程说:
It uses the strong
, instead I read a tutorial on RayWenderlich website that say this:
但对于这两个特殊的属性我还有其他计划。而不是
strong
,我们将其声明为weak
。
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
弱
是所有 outlet 属性的建议关系。
这些视图对象已经是视图控制器视图
层次结构的一部分,不需要在其他地方保留。声明你的网点弱
的巨大优势
是它可以节省你写
viewDidUnload方法的时间。
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.
目前我们的 viewDidUnload
如下所示:
- (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;
}
所以使用弱
,而不是 strong
,并在 videDidUnload
中将设置删除为nil,而不是Xcode使用 strong
,并在 viewDidUnload
中使用 self ... = 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
.
我的问题是:我何时必须使用 strong
,以及何时 weak
?
我还想用于部署目标iOS 4,那么我何时必须使用 unsafe_unretain
?当使用 strong
,弱
和时,任何人都可以通过一个小教程帮助解释我。 unafe_unretain
使用ARC?
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?
推荐答案
经验法则
当父项具有对子对象的引用时,应使用 strong
引用。当一个孩子有对其父对象的引用时,你应该使用弱
引用或 unsafe_unretained
一个(如果前者不可用)。典型的情况是您与代理人打交道。例如, UITableViewDelegate
不会保留包含表视图的控制器类。
When a parent has a reference to a child object, you should use a strong
reference. When a child has a reference to its parent object, you should use a weak
reference or a unsafe_unretained
one (if the former is not available). A typical scenario is when you deal with delegates. For example, a UITableViewDelegate
doesn't retain a controller class that contains a table view.
这是一个简单的架构介绍主要概念。
Here a simple schema to present the main concepts.
假设第一个A,B和C是强
引用。特别是,C对其父级具有 strong
ref。当obj1被释放(某处)时,A引用不再存在但是你有一个泄漏,因为obj1和obj2之间有一个循环。就保留计数而言(仅用于解释目的),obj1的保留计数为2(obj2具有强
引用),而obj2的保留计数为1.如果释放obj1,则其保留计数现在为1,并且不会调用其 dealloc
方法。 obj1和obj2仍然留在记忆中,但没有人对它们有任何引用:泄漏。
Suppose first A,B and C are strong
references. In particular, C has a strong
ref to its parent. When obj1 is released (somewhere), the A reference doesn't exist anymore but you have a leak since there is a cycle between obj1 and obj2. Speaking in terms of retain counts (only for explain purposes), obj1 has a retain count of 2 (obj2 has a strong
reference to it), while obj2 has a retain count of 1. If obj1 is released, its retain count is now 1 and its dealloc
method is not called. obj1 and obj2 still remain in memory but no one has a reference to them: Leak.
在续约中,如果只有A和B是 strong
refs和C被认定为弱
一切正常。你没有泄漏。事实上,当obj1被释放时,它也会释放obj2。就保留计数而言,obj1的保留计数为1,obj2的保留计数为1.如果释放obj1,则其保留计数现在为0,其 dealloc
方法被调用。 obj1和obj2将从内存中删除。
On the contary, if only A and B are strong
refs and C is qualified as weak
all is ok. You have no leaks. In fact, when obj1 is released, it also releases obj2. Speaking in terms of retain counts, obj1 has a retain count of 1, obj2 has a retain count of 1. If obj1 is released, its retain count is now 0 and its dealloc
method is called. obj1 and obj2 are removed from memory.
一个简单的建议:在处理ARC时,开始考虑对象图。
关于第一个问题,两种解决方案在处理XIB时都有效。通常,在处理内存周期时会使用弱
引用。
关于XIBs文件,如果你使用 strong
,你需要在中设置
因为如果你不这样做,在内存不足的情况下,你可能会导致意外泄漏。你不会在 nil
viewDidUnload dealloc
中发布它们,因为ARC会为你做这件事。
弱
而不需要这种处理,因为当目标对象被销毁时,这些值被设置为 nil
自动。没有悬空指针。
About your first question, both solutions are valid when you deal with XIBs. In general weak
references are used when you deal with memory cycles.
Concerning XIBs files, if you use strong
you need to set nil
in viewDidUnload
since if you don't do it, in memory low conditions, you could cause unexpected leaks. You don't release them in dealloc
because ARC will do it for you.
weak
instead doesn't need that treatment since, when the target object is destroyed, those values are set as nil
automatically. No dangling pointers anymore.
如果您有兴趣,我建议您阅读 friday-qa-2012-04-13-nib-memory-management Mike Ash 。
If you are interested in, I really suggest you to read friday-qa-2012-04-13-nib-memory-management by Mike Ash.
关于你的第二个问题,如果你需要支持iOS 4,而不是弱
你有使用 unsafe_unretained
。
About your second question, if you need to support iOS 4, instead of weak
you have to use unsafe_unretained
.
在SO中有很多问题/答案。这里主要是:
Within SO there are a lot of questions/answers. Here the main ones:
< a href =https://stackoverflow.com/questions/6260256/what-kind-of-leaks-does-automatic-reference-counting-in-objective-c-not-prevent/>自动出现什么样的泄漏Objective-C中的引用计数不会阻止或最小化?
使用ARC,终身限定符分配和unsafe_unretained
strong / weak / retain / unsafe_unretained / assign
Hope这有帮助。
更新
根据shaunlim的评论,从iOS 6开始 viewDidUnload
方法已弃用。在这里,我真的建议看看Rob的回答: iOS 6 - viewDidUnload迁移到didReceiveMemoryWarning?。
As per shaunlim's comment, starting from iOS 6 viewDidUnload
method is deprecated. Here I really suggest to see Rob's answer: iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?.
这篇关于IBOutlet和其他弱者或弱者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!