简体   繁体   English

何时使用alloc init

[英]When to use alloc init

The following two examples each seem to work just fine. 以下两个示例似乎都可以正常工作。 In example 1, widget is created before the while loop: 在示例1中, 小部件是在while循环之前创建的:

-(int)compareWidgets { // Example 1

    int count = 0;
    NSMutableArray *widgetsCopy = [self.widgets mutableCopy];

    Widgets *widget = [[Widgets alloc]init]; // HERE ? or...

    while (widgetsCopy.count) {
        widget = [widgetsCopy lastObject]; 
        [widgetsCopy removeLastObject];
        for (Widgets *compareWidget in widgetsCopy)
            if (compareWidget.value == widget.value)
                count += 1;
    }
    return count;

} }

In example 2, widget is created inside the while loop... 在示例2中, 小部件是在while循环内创建的...

-(int)compareWidgets { // Example 2

    int count = 0;
    NSMutableArray *widgetsCopy = [self.widgets mutableCopy];

    while (widgetsCopy.count) {
        Widgets *widget = [widgetsCopy lastObject]; // HERE ?
        [widgetsCopy removeLastObject];
        for (Widgets *compareWidget in widgetsCopy)
            if (compareWidget.value == widget.value)
                count += 1;
    }
    return count;

} }

Q1: In the example 1, widget is allocated/initialized just once and then it seems to be reassigned for each iteration through the array. 问题1:在示例1中, 小部件仅分配/初始化一次,然后似乎通过数组为每次迭代重新分配。 In example 2, alloc/init is never used but widget is, again, successfully assigned through each iteration. 在示例2中,从未使用alloc / init,但是在每次迭代中都成功地分配了小部件 Why is this possible and which example is either correct or preferable? 为什么这是可能的,哪个例子是正确的或可取的?

Q2: Also, in either case, widget is assigned to an array object which is immediately removed from its array: [widgetsCopy removeLastObject] . Q2:同样,在任何一种情况下, 小部件都分配给一个数组对象,该对象立即从其数组中删除: [widgetsCopy removeLastObject] Since the object widget points to has been removed from its array, why isn't widget nil at this point--why does it continue to retain the correct value? 由于对象小部件指向的对象已从其数组中删除,为什么小部件此时不为零-为什么它继续保留正确的值?

Thanks. 谢谢。

The first code snippet creates an object with alloc / init , and then the body of the while immediately releases it. 第一个代码段使用alloc / init创建一个对象,然后while的主体立即释放它。

There is no need to create an object and assign it to your variable in order to make that variable "valid": you can assign the variable as long as it is in scope at the time of the assignment. 无需创建对象并将其分配给您的变量即可使该变量“有效”:您可以分配变量,只要该变量在分配时在范围内即可。

Widget does not become nil right away, because under ARC variables are __strong by default, so an assignment to widget leads to the corresponding object being retained. widget Widget不会立即变为nil ,因为在ARC下,默认情况下__strong是变量,因此对widget的分配会导致保留相应的对象。 If you change Widget *widget to __weak Widget *widget , you would see a nil there, but then there would be no point in declaring that variable in the first place. 如果将Widget *widget更改为__weak Widget *widget ,那么那里会显示nil ,但是首先声明该变量是没有意义的。

Consider pointers as being an empty box. 将指针视为一个空盒子。 In the first example you put a new widget in that box. 在第一个示例中,您在该框中放置了一个新的小部件。 Then the very next thing you do is empty that box and put the lastObject in that box. 然后,接下来要做的是清空该框,然后将lastObject放入该框。 The example breaks down a little since the widget is still in that array as well till you remove the last object. 由于该小部件也仍在该数组中,直到您删除最后一个对象,该示例才出现问题。 ARC lets you not have to worry as much about how many boxes it is in. Once it is taken out of the last box it will get released. ARC让您不必担心它里面有多少盒子。一旦从最后一个盒子中取出它,它就会被释放。

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

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