xcode ios 对强/弱引用感到困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11458286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ios confused about strong/weak references
提问by user1467188
I've looked through the questions on strong/weak references, and understand the reason for using weak (the parent to child relationship). However, I'm confused about specific scenarios where a parent to child relationship is created.
我查看了有关强/弱引用的问题,并了解使用弱(父子关系)的原因。但是,我对创建父子关系的特定场景感到困惑。
For example, is adding subviews to a UIView object..an example of creating a parent/child relationship? What is?
例如,是否将子视图添加到 UIView 对象...创建父/子关系的示例?什么是?
So far, I did everything in my project using strong, nowhere have I used weak, but I'm not sure if I'll run into memory management issues (or how to even check if I will).
到目前为止,我在我的项目中使用strong 做了所有事情,我没有使用过weak,但我不确定我是否会遇到内存管理问题(或者如何检查我是否会遇到)。
Can anyone provide specific situations or examples where a parent to child relationship is created?
任何人都可以提供创建父子关系的具体情况或示例吗?
Thanks!
谢谢!
EDIT: In fact, I am getting some "Received Memory Warning" problems in one of my ViewControllers that displays a lot of data (map view, number of images, text, buttons). Everything property has a strong pointer. I need to fix my memory management issues for this ViewController
编辑:事实上,我在一个显示大量数据(地图视图、图像数量、文本、按钮)的 ViewController 中遇到了一些“收到的内存警告”问题。所有的属性都有一个强指针。我需要修复这个 ViewController 的内存管理问题
回答by Rob Napier
You're understanding is backwards. Weak references are more often used for implementing child-to-parent relationships. They would seldom make sense for a parent-to-child relationship. Generally the parent owns the child; that means strong.
你的理解是倒退的。弱引用更常用于实现子父关系。对于父母与孩子的关系,它们很少有意义。通常父母拥有孩子;这意味着强大。
The vast majority of the time you want a strong reference. That's why it's the default. The most common reason not to have a strong reference is if it would cause a retain loop. For instance, if A has a strong reference to B, then if B had a strong reference to A you'd have a loop and neither object would ever be deallocated. So you pick one of the objects to be the owner, and it has the strong reference. The other object has a weak reference.
绝大多数情况下,您需要一个强有力的参考。这就是为什么它是默认的。没有强引用的最常见原因是它是否会导致保留循环。例如,如果 A 对 B 有一个强引用,那么如果 B 对 A 有一个强引用,你就会有一个循环,并且两个对象都不会被释放。因此,您选择其中一个对象作为所有者,并且它具有强引用。另一个对象具有弱引用。
The most common case of this is delegation. A delegate almost always owns the thing it is delegate for. So the delegating object should have a weak reference to the delegate. As a convention in Objective-C, a property called delegate
is expected to be weak. (If this feels backwards, think about how you use UITableView
and UITableViewDelegate
in practice, and which one you'd want to consider the "owner.")
最常见的情况是委托。委托几乎总是拥有它所委托的东西。所以委托对象应该有一个对委托的弱引用。作为 Objective-C 中的约定,一个名为的属性delegate
应该是弱的。(如果这感觉倒退,请考虑您如何使用UITableView
和UITableViewDelegate
在实践中,以及您希望将哪个视为“所有者”。)
Weak delegate pointers are not a hard-and-fast rule. There are exceptions such as NSURLConnection
. If the delegating object has a shorter lifetime than the delegate, then it is ok (and generally preferable) for it to maintain a strong reference.
弱委托指针不是硬性规定。有例外,例如NSURLConnection
。如果委托对象的生命周期比委托对象的生命周期短,那么它可以(并且通常更可取)保持强引用。
"Received Memory Warning" does not necessarily have anything to do with memory management. It just means you're using too much memory. If you have retain loops, then you may be leaking memory, and that would cause this warning. But it could also be because you're simply using too much memory. The "Allocations" tool in Instruments is the best way to investigate this.
“收到内存警告”不一定与内存管理有关。这只是意味着您使用了太多内存。如果您有保留循环,那么您可能会泄漏内存,这会导致此警告。但这也可能是因为您只是使用了太多内存。Instruments 中的“分配”工具是对此进行调查的最佳方式。
While the implementation of "strong" and "weak" are very recent additions to Objective-C, they just formalize and provide better language support for what properly written code has been doing for many years with manual retains. The ownership patterns are identical today to what they were before ARC.
虽然“强”和“弱”的实现是最近添加到 Objective-C 的,但它们只是形式化并为正确编写的代码多年来使用手动保留所做的事情提供了更好的语言支持。今天的所有权模式与 ARC 之前的所有模式相同。
回答by Rob Napier
Some people put together a very helpful diagram explaining when to use weak references on the "Coding Together" Piazza class. It has some great diagrams explaining basic memory management with strong/weak pointers.
有些人整理了一个非常有用的图表,解释了何时在“一起编码”Piazza 类上使用弱引用。它有一些很好的图表,用强/弱指针解释了基本的内存管理。
回答by bandejapaisa
When you add a subview to a view, the parent will retain it's subview under the covers. Yes this is a parent child relationship. If you are creating your own UIView subclasses, you will be doing the same.
当您向视图添加子视图时,父视图将在幕后保留其子视图。是的,这是父子关系。如果您正在创建自己的 UIView 子类,您也会这样做。
Parents have a strong relationship with their children, but children should never have a strong relationship on their parents. This can lead to retain cycles, where neither can be released because they both have a strong connection with one another.
父母与孩子的关系很牢固,但孩子永远不应该对父母有很强的关系。这可能导致保留循环,其中任何一个都不能被释放,因为它们彼此之间有很强的联系。
Strong is the default, you will tend to use weak when you are using the delegate pattern.
Strong 是默认设置,当您使用委托模式时,您将倾向于使用 weak。
Maybe you should post more specific info about the problem you are having with the received memory warning, as this question is quite general about memory semantics.
也许您应该发布有关您收到的内存警告所遇到的问题的更具体的信息,因为这个问题是关于内存语义的非常普遍的问题。