NSMutablearray 不打印所有数组元素
NSMutablearray doesn't prints all array elements
我正在使用以下代码将元素动态存储在数组中并稍后检索它
for (int i = 0; i< [companyNames count]; i++){
testimonialsArray = [[NSMutableArray alloc]init];
testimonialsComplementedArray = [[NSMutableArray alloc]init];
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
NSLog(@"Compliments %@",testimonialsComplementedArray);
}
但它只打印添加到数组中的最后一个副本。如何检索所有元素?
以下是公司名称,
"General Marketing Company",
"United Enterprises, Chennai",
"Hari Match Industries"
testimonialsArray = [[NSMutableArray alloc]init]; // have to init out side the loop
testimonialsComplementedArray = [[NSMutableArray alloc]init]; // have to init out side the loop
for (int i = 0; i< [companyNames count]; i++){
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
}
NSLog(@"Compliments %@",testimonialsComplementedArray);
在for循环上面分配testimonialsArray = [[NSMutableArray alloc]init];
如果你每次都在循环内部初始化内存将被初始化,最终值只有你在finally中输出,就像
testimonialsArray = [[NSMutableArray alloc]init];
testimonialsComplementedArray = [[NSMutableArray alloc]init]
for (int i = 0; i< [companyNames count]; i++){
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
}
NSLog(@"Compliments %@",testimonialsComplementedArray);
你每次都在 for 循环中分配可变数组,因为它会初始化数组并在其中添加对象 everytime.Always 最好在 ViewDidLoad 方法中分配任何对象。
这样试试。
testimonialsArray = [[NSMutableArray alloc]init];
testimonialsComplementedArray = [[NSMutableArray alloc]init];
for (int i = 0; i< [companyNames count]; i++){
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
}
NSLog(@"Compliments %@",testimonialsComplementedArray);
我正在使用以下代码将元素动态存储在数组中并稍后检索它
for (int i = 0; i< [companyNames count]; i++){
testimonialsArray = [[NSMutableArray alloc]init];
testimonialsComplementedArray = [[NSMutableArray alloc]init];
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
NSLog(@"Compliments %@",testimonialsComplementedArray);
}
但它只打印添加到数组中的最后一个副本。如何检索所有元素?
以下是公司名称,
"General Marketing Company",
"United Enterprises, Chennai",
"Hari Match Industries"
testimonialsArray = [[NSMutableArray alloc]init]; // have to init out side the loop
testimonialsComplementedArray = [[NSMutableArray alloc]init]; // have to init out side the loop
for (int i = 0; i< [companyNames count]; i++){
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
}
NSLog(@"Compliments %@",testimonialsComplementedArray);
在for循环上面分配testimonialsArray = [[NSMutableArray alloc]init];
如果你每次都在循环内部初始化内存将被初始化,最终值只有你在finally中输出,就像
testimonialsArray = [[NSMutableArray alloc]init];
testimonialsComplementedArray = [[NSMutableArray alloc]init]
for (int i = 0; i< [companyNames count]; i++){
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
}
NSLog(@"Compliments %@",testimonialsComplementedArray);
你每次都在 for 循环中分配可变数组,因为它会初始化数组并在其中添加对象 everytime.Always 最好在 ViewDidLoad 方法中分配任何对象。
这样试试。
testimonialsArray = [[NSMutableArray alloc]init];
testimonialsComplementedArray = [[NSMutableArray alloc]init];
for (int i = 0; i< [companyNames count]; i++){
[testimonialsArray addObject:companyNames[i] ];
[testimonialsComplementedArray addObject:texts[i]];
}
NSLog(@"Compliments %@",testimonialsComplementedArray);