使用ARC(强/弱)避免循环保留,学习一些基础知识 [英] Avoiding circular retention using ARC (strong/weak), learning some basics

查看:118
本文介绍了使用ARC(强/弱)避免循环保留,学习一些基础知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎还没遇到问题,但我正在努力确保使用一些最佳做法。

I haven't seemed to run into a problem yet, but I'm trying to make sure I'm using some best practices.

说我有一个问题UITableViewController,带有MyObject对象的NSArray数据源。所以在我的UITableViewController中,我声明我的数据源如:

Say I have a UITableViewController with a data source of an NSArray of MyObject objects. So in my UITableViewController I declare my data source like:

@property (strong, nonatomic) NSArray *dataSource;

然后我触摸一个单元格后,我想推出一个显示某个细节视图的新视图,使用该单元格的MyObject。所以在新的UIViewController中我有这个:

Then after I touch a cell I want to push a new view that shows a detail view of something, using that cell's MyObject. So in the new UIViewController I have this:

@property (strong, nonatomic) MyObject *myObject;

当触摸单元格时,现在在UITableViewController中:

Now in the UITableViewController when a cell is touched:

MyObject *myObject = [[self dataSource] objectForIndex:[indexPath row]];
NewView *view = [[NewView alloc] initWithMyObject:myObject];
// ... push onto nav controller, etc

基本上我害怕什么我的数组是用声明的,详细视图中的MyObject是用声明的,谁知道可能有另一个视图使用声明了相同的MyObject强大

Basically what I'm afraid of is my array is declared with strong, MyObject in the detailed view is declared with strong, and who knows maybe there is another view with the same MyObject declared with strong.

底线:这是在视图之间传递对象的正确方法吗?我还没有在我的应用程序中使用弱引用,我觉得这是不对的。任何帮助或帮助链接都会很棒。

Bottom line: is this the proper way to pass an object in between views? I haven't really used a weak reference yet in my apps and I feel like that isn't right. Any help or links to help would be amazing.

推荐答案

我认为您需要了解的是弧的工作原理。

I think that what you need to understand is how arc works.

基本上任何指向它的强指针都会保留。

Basically whatever has a strong pointer pointing to it will be retained.

这可以通过添加一个参考计数器来实现。对象所以当你这样做时:

This works by adding a reference counter in the object so when you do this:

@property (strong, nonatomic) MyObject *myObject;

为myObject创建一个STRONG指针(不是对象)。

you create a STRONG pointer for myObject, (not the object).

但是当你这样做时

MyObject *myObject = [[self dataSource] objectForIndex:[indexPath row]];

你使这个指针增加引用计数来自该数据源的指定索引中的任何内容。

you make this pointer increase the reference counting on whatever you have in the specified index from that data source.

重要的是,只要指针指向这个对象,它就会保持活着。

The important part is that as long as the pointer keeps pointing to this object it will be kept alive.

关于您对视图的关注。

在界面构建器中创建的视图使用强指针在内部声明其元素。这是您想要使用引用的时候。当您将自己的IBOutlet添加到视图中的元素时,最好将其设置为弱。如果你从逻辑上考虑原因,它基本上意味着你不关心这个接口构建器元素,因为你只希望它存活直到取消分配viewcontroller。

Views created in the interface builder have their elements declared internally with strong pointers. This is when you want to use a weak reference. When you add your own IBOutlet to an element in the view it is good practice to make it weak. If you think about the reason logically, it basically means that you dont care about this interface builder element since you only want it to survive until the viewcontroller is deallocated.

当你通常遇到保留周期是指对象具有子对象,并且此子对象具有对其父对象的 STRONG 引用。

When you usually encounter retain cycles is when an object has a child object, and this child object has a STRONG reference to its parent.

这是:

对象 A 使用指针创建对象 B

Object A creates object B with a strong pointer

对象 B 指向具有指针的对象 A

Object B points to object A with a strong pointer

A 会使 B 保持活力, B 会使 A 保持活着。

A will keep B alive and B will keep A alive.

这个页面将向你解释一些关于如何避免这类东西的基本知识:

This page will explain to you some basic stuff about how to avoid this kind of stuff:

http://cocoawithlove.com/20 09/07 / rules-to-avoid-retain-cycles.html

关于在视图之间传递对象,它非常简单。

Also about passing objects between views, it is very very simple.

首先得到一个从View 1到View 2的指针(可以强或弱,取决于谁应该保持视图2活着,如果它来自IB Builder它应该是弱的,如果它以编程方式它应该是强的)

First get a pointer from View 1 to View 2 (can be strong or weak depending on who should be keeping view 2 alive, if its from the IB Builder it should be weak if its programatically it should be strong)

其次,在视图2中创建一个属性(@property(强,非原子)MyObject * myObject;)

Second, make a property in view 2 (@property (strong, nonatomic) MyObject *myObject;)

现在它很简单:

Self.view1Pointer.myObject = self.myOtherObject;

在此了解两个视图如何强烈指向此对象,以便对象保持活动状态其中一个视图尚未解除分配。

Understand here how both views are strongly pointing to this object so the object will be kept alive as long as 1 of the views hasnt been deallocated.

您不会创建保留周期,只需将该对象的引用计数设置为2.

You wont create a retain cycle, you simply have the reference counting from that object set to 2.

注意:当视图被释放时,它的所有指针都设置为nil,因此它们指向的任何对象的引用计数都会减少。如果它达到0则取消分配。 (在之前的情况下,myobject将为1,因为另一个视图仍然指向它)。

Note: When a view is deallocated, all of its pointers are set to nil so any object being pointed by them will decrease in its reference count. IF it reaches 0 it is deallocated. (in the previous case myobject will be 1 because another view is still pointing to it).

您创建保留周期的唯一方案是,如果您设法制作myObject也将强烈指向View2。所以现在他们互相活着。 (但如前所述,你可以让myObject指向view2 ,这不会创建一个保留周期)。

The only scenario where you will create a retain cycle is if you manage to make myObject point strongly to View2 as well. So now they are keeping each other alive. (but as explained before you can make myObject point to view2 weakly which wont create a retain cycle).

你可以在这里了解更多关于arc的信息:

You can learn more about arc here:

http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

这篇关于使用ARC(强/弱)避免循环保留,学习一些基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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