ios对强/弱参考感到困惑 [英] ios confused about strong/weak references

查看:130
本文介绍了ios对强/弱参考感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了关于强/弱引用的问题,并了解使用弱(父对子关系)的原因。但是,我对创建父对子关系的特定场景感到困惑。

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.

例如,是否将子视图添加到UIView对象中...创建父/子关系的示例?是什么?

For example, is adding subviews to a UIView object..an example of creating a parent/child relationship? What is?

到目前为止,我在项目中使用强项完成了所有工作,无处使用弱,但我不确定是否会遇到内存管理问题(或者如何检查我是否会)。

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).

任何人都可以提供创建父母与子女关系的特定情况或示例吗?

Can anyone provide specific situations or examples where a parent to child relationship is created?

谢谢!

编辑:事实上,我收到了一些收到内存警告问题我的ViewControllers显示大量数据(地图视图,图像数量,文本,按钮)。 Everything属性有一个强大的指针。我需要为这个ViewController修复我的内存管理问题

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

推荐答案

你的理解是向后的。弱引用更常用于实现子到父关系。对于父母与孩子的关系,他们很少有意义。一般来说,父母拥有孩子;这意味着强大。

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.

绝大多数时候你想要一个强大的参考。这就是为什么它是默认的。没有强引用的最常见原因是它是否会导致保留循环。例如,如果A具有对B的强引用,那么如果B具有对A的强引用,则您将有一个循环,并且这两个对象都不会被释放。因此,您选择其中一个对象作为所有者,并且它具有强大的参考。另一个对象有一个弱引用。

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.

最常见的情况是委托。代表几乎总是拥有委托给它的东西。因此委托对象应该具有对委托的弱引用。作为Objective-C中的约定,一个名为 delegate 的属性预计会很弱。 (如果这感觉倒退,请考虑一下如何在实践中使用 UITableView UITableViewDelegate ,以及您想要的考虑所有者。)

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.")

弱委托指针不是一个严格的规则。有一些例外,例如 NSURLConnection 。如果委托对象的生命周期比委托更短,那么它可以(并且通常更可取)来保持强引用。

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.

收到的内存警告确实如此不一定与内存管理有任何关系。这只是意味着你使用了太多的内存。如果你有保留循环,那么你可能会泄漏内存,这将导致此警告。但也可能是因为你只是在使用太多的内存。 Instruments中的Allocations工具是调查此问题的最佳方法。

"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.

虽然强和弱的实现是Objective-C的最新补充,他们只是正式化并为手动保留多年来正确编写的代码提供了更好的语言支持。今天的所有权模式与ARC之前的相同。

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.

这篇关于ios对强/弱参考感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆