简体   繁体   English

ARC关于网点和网点集合的弱引用和强引用?

[英]ARC weak and strong references with outlets and collection of outlets?

I would have more information about ARC and weak and strong reference : 我将获得有关ARC的更多信息,以及薄弱的参考资料:

Actually, if I have : 实际上,如果我有:

@interface
@property (weak) IBOutlet UIButton * button
@property (weak) UIView *subview
@end

@implementation

-(BOOL) viewDidLoad
{
    UIView *aSubView= [[UIView alloc]....];
    [self.view addSubview:aSubview];
    self.subview = aSubview;
}

It's normal to have weak reference for the button because its superview have a strong reference on it. 对按钮有较弱的引用是正常的,因为其超级视图对此按钮具有较强的引用。

Now, I add UIView programmatically, I also put a weak reference because when I will add this subView in a superview, there will be a strong reference. 现在,我以编程方式添加UIView ,我也放置了一个弱引用,因为当我将这个subView添加到超级视图中时,将会有一个强引用。 First question : Is this a good method ? 第一个问题:这是一个好方法吗?

Now my real problems are on the second source code with the collection. 现在,我的真正问题出在该集合的第二个源代码上。 What can I put with IBOutletCollection ? 我可以在IBOutletCollection放什么?

And if I want to keep an array of views which are added programmatically I can't because NSArray keep strong reference and views' superview too so there will be some leaks . 而且,如果我想保留以编程方式添加的一系列视图,则不能这样做,因为NSArray保留了强大的引用和视图的超级视图,因此会出现一些泄漏。 How can I have a NSArray of my subviews without leaks ? 我如何在不泄漏的情况下拥有子视图的NSArray

@property (?) IBOutletCollection .....
@property (?) NSArray *subviews

-(BOOL) viewDidLoad
{
    ?
}

Outlets are usually weak references because the views are owned by their superviews. 出口通常是弱引用,因为视图由其超级视图拥有。 If you make them a weak reference, all you have to do to get rid of a view object is remove it from it's superview, and the outlet gets zeroed out. 如果使它们成为弱引用,则要摆脱视图对象,您所需要做的就是将其从其父视图中删除,并且出口将被清零。

Your example of a subview that you create programmatically is the same thing, and making it weak is a good idea. 以编程方式创建的子视图示例是同一回事,而使其变弱则是个好主意。

Your NSArray of subviews needs to be a strong reference or the array will be released. 您的NSArray子视图需要成为强引用,否则将发布该数组。 Same goes with an IBOutletCollection, which is really just an array maintained by the system. IBOutletCollection也是如此,它实际上只是系统维护的数组。

You will need to remove your views from these arrays yourself if you want them to be released prior to the owning view controller being released. 如果要在释放拥有的视图控制器之前释放它们,则需要自己从这些数组中删除视图。

You could probably create your own equivalent of an outlet collection using an NSPointerArray, which does not retain the pointers that you pass to it. 您可能可以使用NSPointerArray创建自己的奥特莱斯集合等效项,它不会保留传递给它的指针。 However, you would need to be careful because it also does not zero out items that are released, so you would get zombies if you removed items from your view but did not remove their entry in the NSPointerArray. 但是,您将需要小心,因为它也不会将释放的项目归零,因此,如果您从视图中删除了项目但未删除其在NSPointerArray中的条目,则您将得到僵尸。

All things considered, I would suggest just using a regular mutable array and doing your own housekeeping on the contents to remove items from the array if you remove them from their superview. 考虑所有事情,我建议只使用一个常规的可变数组,并对内容进行自己的整理,以将其从数组的超级视图中删除。

To your first question, yes that is the recommended strategy for subview properties. 对于第一个问题,是的,这是推荐的子视图属性策略。 As to the second question, I'm not exactly sure what you are asking. 关于第二个问题,我不确定您要问什么。 Here's a good tutorial on IBOutletCollection if that's what you're looking for. 如果您正在寻找IBOutletCollection,那么这是一个很好的教程 If not, please clarify what you're problem with IBOutletCollection is. 如果不是,请说明IBOutletCollection存在的问题。

1) When you add a subview programatically, you can keep a weak reference as a property in your view controller. 1)以编程方式添加子视图时,可以将弱引用作为属性保留在视图控制器中。

2) IBOutletCollection is actually removed by the preprocessor and doesn't mean anything to the compiler. 2)IBOutletCollection实际上是由预处理器删除的,对编译器没有任何意义。 It's just a hint within XCode that there's an outlet collection associated with the property. 这只是XCode中的提示,即与该属性相关联的插座集合。

3) Here's a page that talks about using NSArray to store weak references: 3)这是讨论使用NSArray存储弱引用的页面:

NSArray of weak references (__unsafe_unretained) to objects under ARC 对ARC下的对象的弱引用(__unsafe_unretained)的NSArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM