我试图了解在 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 种情况下都没有崩溃,但我想阅读关于我应该在哪里分配大量数组的说明:这是同一件事吗?还是在性能和内存分配方面有一个最佳位置,以避免一些问题?谢谢!