[英]Alloc and init NSArrays inside or outside a parsing function in Objective-C?
我试图了解WHERE更好地在Objective-C ARC应用程序中分配数组:
案例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];
}
}
案例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];
}
}
案例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];
}
}
好吧,我的应用程序在所有3种情况下都不会崩溃,但是我想阅读一份说明 ,我应该在哪里分配大量数组:是同一件事吗? 还是在性能和内存分配方面处于最佳位置以避免某些问题? 谢谢!
第三是不正确的。 您正在多次创建/分配所有数组,并且仅存储最后一个。
第一和第二是相当不错的。 这取决于您的要求。 如果确定在调用该方法时只需要一次这些数组,而在其他地方则不需要。 那么2就好了。 但是,如果您两次调用它,它将类似于3。
但是,所有属性都在viewDidLoad方法中初始化。
情况3:
完全不正确:在每次迭代中,您都在“重置”数组内容; 在循环的最后,您的数组将仅包含您添加的最后一个元素;
情况1:
您可以在视图生命周期中一次初始化数组; 然后, 每次执行parse函数时,都会向这些相同的数组添加新元素; 当您继续调用parse方法时,数组将变得越来越大,并且包含所有解析的“历史记录”;
情况2:
每次输入解析函数时,都将“重置”数组,然后用新元素填充它们; 在循环结束时,数组将仅包含最后一个解析任务的结果。
因此,在1到2之间,取决于您要尝试执行的操作。 这两件事都说得通,我敢打赌2。
编辑:
回答您的其他问题(评论):
谢谢@sergio,到目前为止,您的是最好的解释。 但是在ViewController内分配和初始化数组的危险和问题是什么?
我不会谈论危险或问题:它可以完美运行,至少对于简单的应用程序而言。
另一方面,假设您想在2级视图中显示解析的内容:首先是一个项目列表(想象一个表);然后是一个列表。 然后,当您选择一个项目时,将移至另一视图,显示有关该项目的更多详细信息。
在这种情况下,您将有2个不同的控制器需要访问相同的数据。 如果阵列仅由一个控制器管理,则另一个控制器将依赖于此。 如果您添加更多的控制器和视图来访问相同的数据,事情可能会变得更加复杂。
因此,这是“好的设计”和为更改做好准备的问题:如果通过ad-hoc类(模型-视图-控制器中的模型)来管理数据,则会得到更加整洁有序的依赖关系图。
希望这可以帮助。
或者您可以设法在字典数组中完成所有这些操作,那么您就不必跟踪N个数据源数组:
您可以做的是:
MasterArray->保留所有子阵列的键->在键上添加子阵列数据。 并动态地取消分配这些子数组,因此在内存中您将只有一个masterArray,而没有N个数据源数组。
例如:
MasterArray {'0'=({一些数组对象}};
'1'=({一些数组对象)};
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.