我正在向另一个NSMutableArray添加一个NSMutableArray,问题是第一个数组中的所有对象都是相同的(长度、大小、内容等)。我猜想当你将一个数组添加到一个数组中时,第一个数组简单地持有一个指向第二个数组的指针,那么我如何让它持有一个唯一的数组呢?我想我需要在添加的时候使用arrayWithArray,但是我不能理解语法。
我的NSDictionary包含许多对象,每个对象都加载了图像URL,然后下载这些URL。
到目前为止我的代码;
for (NSDictionary *obj in MyDictList)
{
[tempImageArray removeAllObjects];
for(NSString *tempImageURL in obj[@"images"])
{
tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:tempImageURL]]];
NSLog(@"Download Extra Image : %@, %i", tempImageURL, [UIImagePNGRepresentation(tempImage) length]);
[tempImageArray addObject:tempImage];
}
NSLog(@"Number of pics fo this event : %i", [tempImageArray count]);
// Add the array of images to the array
[eventImages addObject:tempImageArray];
}日志记录了这一点(因为您可以看到每个图像的URL和大小不同)。
Download Extra Image : http://.....A...Correct...URL/file.jpg, 69516
Download Extra Image : http://.....A...Correct...URL/file.jpg, 63263
Number of pics fo this event : 2
Download Extra Image : http://.....A...Correct...URL/file.jpg, 69516
Download Extra Image : http://.....A...Correct...URL/file.jpg, 64545
Number of pics fo this event : 2
Download Extra Image : http://.....A...Correct...URL/file.jpg, 56541
Download Extra Image : http://.....A...Correct...URL/file.jpg, 69144
Download Extra Image : http://.....A...Correct...URL/file.jpg, 51585
Number of pics fo this event : 3
Download Extra Image : http://.....A...Correct...URL/file.jpg, 56813
Download Extra Image : http://.....A...Correct...URL/file.jpg, 33869
Number of pics fo this event : 2当我循环它们时,我得到了最后一个数组的4个副本(即只有2张照片)。
Number of image in this Event at Row : 2, 0
Number of image in this Event at Row : 2, 1
Number of image in this Event at Row : 2, 2
Number of image in this Event at Row : 2, 3编辑感谢帮助,向正确的方向轻推,并将最后一行改为阅读;
[eventImages addObject:[NSArray arrayWithArray:tempImageArray]];发布于 2013-07-05 20:44:40
问题是你不应该使用removeAllObjects,因为它只是清理了数组(删除了你刚刚做的工作)。相反,您应该创建一个新数组(tempImageArray = [NSMutableArray array];)。
发布于 2013-07-05 20:51:50
所以你想把它们组合起来?你可以这样做:
NSMutableArray *m1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableArray *m2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", nil];
for (id obj in m2)
[m1 addObject:obj];(不确定这是不是你的问题)
https://stackoverflow.com/questions/17489090
复制相似问题