简体   繁体   English

在Object-C中的解析函数内部还是外部分配和初始化NSArrays?

[英]Alloc and init NSArrays inside or outside a parsing function in Objective-C?

Im trying to understand WHERE is better to allocate my arrays, in a Objective-C ARC application: 我试图了解WHERE更好地在Objective-C ARC应用程序中分配数组:

CASE 1 : alloc and init arrays outside parsing function 案例1 :解析函数之外的alloc和init数组

- (void)viewDidLoad { // or another method

    // other code
    // here I alloc and init arrays:

    dataSource2 = [[NSMutableArray alloc] init]; 
    dataSource3 = [[NSMutableArray alloc] init]; 
    dataSource4 = [[NSMutableArray alloc] init]; 
    dataSource5 = [[NSMutableArray alloc] init]; 
    dataSource6 = [[NSMutableArray alloc] init]; 
    dataSource7 = [[NSMutableArray alloc] init]; 
    dataSource8 = [[NSMutableArray alloc] init]; 
    dataSource9 = [[NSMutableArray alloc] init]; 
}

- (void)parseFunction {

    // parsing code
    // do something with arrays and iterate, example: 

       for (int i = 1; i < [arrays count]; ++i) {
            [dataSource2 addObject:object2];
            [dataSource3 addObject:object3];
            [dataSource4 addObject:object4];
            [dataSource5 addObject:object5];
            [dataSource6 addObject:object6];
            [dataSource7 addObject:object7];
            [dataSource8 addObject:object8];
            [dataSource9 addObject:object9];
        }
}

CASE 2 : alloc and init arrays inside parsing function and outside iteration cycle 案例2 :解析函数内部和迭代周期外部的alloc和init数组

- (void)viewDidLoad { // or another method

    // other code
}

- (void)parseFunction {

    // here I alloc and init arrays: 

    dataSource2 = [[NSMutableArray alloc] init]; 
    dataSource3 = [[NSMutableArray alloc] init]; 
    dataSource4 = [[NSMutableArray alloc] init]; 
    dataSource5 = [[NSMutableArray alloc] init]; 
    dataSource6 = [[NSMutableArray alloc] init]; 
    dataSource7 = [[NSMutableArray alloc] init]; 
    dataSource8 = [[NSMutableArray alloc] init]; 
    dataSource9 = [[NSMutableArray alloc] init]; 

    // parsing code
    // do something with arrays and iterate, example:

       for (int i = 1; i < [arrays count]; ++i) {

            [dataSource2 addObject:object2];
            [dataSource3 addObject:object3];
            [dataSource4 addObject:object4];
            [dataSource5 addObject:object5];
            [dataSource6 addObject:object6];
            [dataSource7 addObject:object7];
            [dataSource8 addObject:object8];
            [dataSource9 addObject:object9];
        }
}

CASE 3 : alloc and init arrays inside parsing function and inside iteration cycle 案例3 :解析函数内部和迭代周期内部的alloc和init数组

- (void)viewDidLoad { // or another method

    // other code
}

- (void)parseFunction {

    // parsing code, alloc init and iterate, all in the same cycle:

       for (int i = 1; i < [arrays count]; ++i) {

       dataSource2 = [[NSMutableArray alloc] init]; 
       dataSource3 = [[NSMutableArray alloc] init]; 
       dataSource4 = [[NSMutableArray alloc] init]; 
       dataSource5 = [[NSMutableArray alloc] init]; 
       dataSource6 = [[NSMutableArray alloc] init]; 
       dataSource7 = [[NSMutableArray alloc] init]; 
       dataSource8 = [[NSMutableArray alloc] init]; 
       dataSource9 = [[NSMutableArray alloc] init]; 

            [dataSource2 addObject:object2];
            [dataSource3 addObject:object3];
            [dataSource4 addObject:object4];
            [dataSource5 addObject:object5];
            [dataSource6 addObject:object6];
            [dataSource7 addObject:object7];
            [dataSource8 addObject:object8];
            [dataSource9 addObject:object9];
        }
}

Well, my application works without crashes in all 3 cases, but I'd like to read an explanation of where should I allocate a great number of arrays: is it the same thing? 好吧,我的应用程序在所有3种情况下都不会崩溃,但是我想阅读一份说明 ,我应该在哪里分配大量数组:是同一件事吗? or is there a BEST position, in terms of performance and memory allocation, to avoid some issues? 还是在性能和​​内存分配方面处于最佳位置以避免某些问题? Thanks! 谢谢!

3rd is Incorrect. 第三是不正确的。 You are creating / allocating all the arrays multiple times and storing only last one. 您正在多次创建/分配所有数组,并且仅存储最后一个。

1st and 2nd are quite good. 第一和第二是相当不错的。 It depends on your requirement. 这取决于您的要求。 If you are sure that these array will only be required only once when you call the method and not else where. 如果确定在调用该方法时只需要一次这些数组,而在其他地方则不需要。 Then 2 is fine. 那么2就好了。 But if you call it twice then it will be like 3. 但是,如果您两次调用它,它将类似于3。

However all properties are initialized in viewDidLoad method. 但是,所有属性都在viewDidLoad方法中初始化。

CASE 3: 情况3:

utterly incorrect: at each iteration you are "resetting" the arrays content; 完全不正确:在每次迭代中,您都在“重置”数组内容; at the end of the loop, your arrays will only contain the last element you added; 在循环的最后,您的数组将仅包含您添加的最后一个元素;

CASE 1: 情况1:

you initialize you arrays once in your view lifetime; 您可以在视图生命周期中一次初始化数组; then each time you execute the parse function you are adding new elements to those same arrays; 然后, 每次执行parse函数时,都会向这些相同的数组添加新元素; the arrays will grow larger and larger as you keep calling the parse method and will contain, say, the "history" of all your parsing; 当您继续调用parse方法时,数组将变得越来越大,并且包含所有解析的“历史记录”;

CASE 2: 情况2:

each time you enter the parse function, you "reset" the arrays, then fill them up with new elements; 每次输入解析函数时,都将“重置”数组,然后用新元素填充它们; at the end of the loop the arrays will only contain the results of the last parse task. 在循环结束时,数组将仅包含最后一个解析任务的结果。

So, between 1 and 2, it depends on what you are trying to do; 因此,在1到2之间,取决于您要尝试执行的操作。 both things can make sense, and I would bet for 2. 这两件事都说得通,我敢打赌2。

EDIT: 编辑:

In reply to your other question (comments): 回答您的其他问题(评论):

Thank you @sergio, until now your's is the best explanation; 谢谢@sergio,到目前为止,您的是最好的解释。 but what is the danger and issue of allocating and initialing arrays INSIDE a ViewController? 但是在ViewController内分配和初始化数组的危险和问题是什么?

I would not talk of danger or issues: it can work perfectly fine, at least for simple apps. 我不会谈论危险或问题:它可以完美运行,至少对于简单的应用程序而言。

On the other hand, imagine you want to display the content you parse in a 2-level view: first a list of items (imagine a table); 另一方面,假设您想在2级视图中显示解析的内容:首先是一个项目列表(想象一个表);然后是一个列表。 then, when you select one item, you move to another view displaying some more details about that item. 然后,当您选择一个项目时,将移至另一视图,显示有关该项目的更多详细信息。

In this case, you would have 2 different controllers requiring access to the same data. 在这种情况下,您将有2个不同的控制器需要访问相同的数据。 If you array is managed by just one controller, then the other is made dependent on this. 如果阵列仅由一个控制器管理,则另一个控制器将依赖于此。 And the thing could become more complex if you add more controllers and views accessing the same data. 如果您添加更多的控制器和视图来访问相同的数据,事情可能会变得更加复杂。

Thus, it is a question of "good design" and being prepared for change: if you make your data managed through and ad-hoc class (the model in model-view-controller), you get a much cleaner and orderly graph of dependencies. 因此,这是“好的设计”和为更改做好准备的问题:如果通过ad-hoc类(模型-视图-控制器中的模型)来管理数据,则会得到更加整洁有序的依赖关系图。

Hope this helps. 希望这可以帮助。

As the others said, the third option is completely wrong. 正如其他人所说,第三种选择是完全错误的。 But the other two options are not very good either. 但是其他两个选项也不是很好。 You really should not have your data model and parsing code in a view controller. 您实际上不应该在视图控制器中拥有数据模型和解析代码。 Create one class to handle your data model in which you do all the parsing and pass that to your view controller, for example in your init method. 创建一个类来处理您的数据模型,在其中进行所有解析并将其传递给视图控制器,例如在init方法中。 Don't have your view controller create that data model object. 不要让您的视图控制器创建该数据模型对象。

Read about the Single Responsibility Principle and Dependency Injection . 阅读有关单一责任原则依赖注入的信息

Or you can manage to do all of these things in array of dictionaries then you don't have to keep track of N datasource arrays: 或者您可以设法在字典数组中完成所有这些操作,那么您就不必跟踪N个数据源数组:

You can do is: 您可以做的是:

MasterArray -> Holding Keys of All sub arrays -> Add sub arrays data on a key. MasterArray->保留所有子阵列的键->在键上添加子阵列数据。 and dealloc these subarrays dynamically, so in memory you will only have one masterArray but not N datasource arrays. 并动态地取消分配这些子数组,因此在内存中您将只有一个masterArray,而没有N个数据源数组。

Eg: 例如:

MasterArray { '0' = ( { some array Objects ) }; MasterArray {'0'=({一些数组对象}};

'1' = ( { some array Objects ) }; '1'=({一些数组对象)};

Hope it helps. 希望能帮助到你。

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

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