简体   繁体   English

使用ARC时重新分配属性

[英]Reassigning properties when using ARC

I have a form with various controls. 我有一个带有各种控件的表单。 One of these controls lets the user change the category of the form being submitted via a modal view that appears after tapping on the control. 这些控件之一使用户可以通过在点击控件后出现的模式视图来更改要提交的表单的类别。 When the user makes a selection to change the category of the form, the form needs to re-display the controls based on the new category. 当用户选择更改表单的类别时,表单需要根据新的类别重新显示控件。

Other than removing the previous controls from the form's view, is there anything I need to worry about in regards to the controls that are being discarded? 除了从窗体的视图中删除以前的控件之外,对于要丢弃的控件,我是否还需要担心什么? Consider the following method that the form implements: 考虑表单实现的以下方法:

- (void)showControls
    self.controls = [NSMutableArray array];

    for(UIControl *control in self.dataSource.controls){
        [self.controls addObject:control];
        [self.view addSubview:control];
    }
}

If I call this method multiple times (because the user changes the form category, as described earlier), I assume it results in various NSMutableArrays floating around without any pointers that reference them. 如果我多次调用此方法(因为用户更改了表单类别,如前所述),我认为它将导致各种NSMutableArray浮动,而没有任何引用它们的指针。 Is this ok to do? 这样可以吗? I'm using ARC, so will it automatically collect those "lost" arrays? 我正在使用ARC,因此它会自动收集那些“丢失的”阵列吗?

ARC doesn't "collect" anything. ARC不“收集”任何东西。 Also, what you're doing there is fairly irrelevant that ARC is being used. 另外,您在此处所做的操作与使用ARC完全无关。 You're using the setter for the control property which will work in the same way as pre-ARC. 您正在将setter用于control属性,该属性将与ARC之前的版本相同。 It will release the old value and retain the new value. 它将释放旧值并保留新值。 So, you shouldn't have any problems with the arrays being "lost", unless you do weird things with self.controls other than what you've shown. 因此,除非丢失了数组,否则您不会对数组“丢失”有任何问题,除非您对self.controls进行了奇怪的处理。

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

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