Objective c Xcode将NSMutableArray添加到NSMutableArray
我正在将一个NSMutableArray添加到另一个NSMutableArray,问题是第一个数组中的所有对象都是相同的(长度、大小、内容等)。我猜当你向数组中添加一个数组时,第一个简单数组会保存一个指向第二个简单数组的指针,那么如何让它保存一个唯一的数组呢?我想在添加时需要使用arrayWithArray,但我无法理解语法 我的NSDictionary包含许多对象,每个对象都有一堆图像URL,然后下载 迄今为止我的代码Objective c Xcode将NSMutableArray添加到NSMutableArray,objective-c,nsmutablearray,multidimensional-array,Objective C,Nsmutablearray,Multidimensional Array,我正在将一个NSMutableArray添加到另一个NSMutableArray,问题是第一个数组中的所有对象都是相同的(长度、大小、内容等)。我猜当你向数组中添加一个数组时,第一个简单数组会保存一个指向第二个简单数组的指针,那么如何让它保存一个唯一的数组呢?我想在添加时需要使用arrayWithArray,但我无法理解语法 我的NSDictionary包含许多对象,每个对象都有一堆图像URL,然后下载 迄今为止我的代码 for (NSDictionary *obj in MyDictList)
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和大小)
当我在它们之间循环时,我得到了最后一个阵列的4个副本(即仅2张图片)
编辑
感谢您的帮助,向正确的方向轻推,并将最后一行改为阅读
[eventImages addObject:[NSArray arrayWithArray:tempImageArray]];
问题是,您不应该使用
removeAllObjects
,因为它只会清除数组(删除您刚才所做的工作)。相反,您应该创建一个新数组(tempmagearray=[NSMutableArray];
)。问题是,您不应该使用removeAllObjects
,因为它只是清除数组(删除您刚才所做的工作)。相反,您应该创建一个新数组(tempagearray=[NSMutableArray];
)。那么您想合并它们吗?您可以这样做:
NSMutableArray *m1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableArray *m2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", nil];
for (id obj in m2)
[m1 addObject:obj];
(不确定这是否是您的问题)那么您想合并它们吗?您可以这样做:
NSMutableArray *m1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableArray *m2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", nil];
for (id obj in m2)
[m1 addObject:obj];
(不确定这是否是你的问题)好的,听起来不错,但我不知道我的字典中有多少个对象,或者每个对象中有多少个图像。如何创建数量未知的数组?您希望
eventImages
成为一个图像数组或图像数组?是的,我得到了您的想法,我刚刚用此行[eventImages addObject:[NSArray arrayWithArray:tempImageArray]]替换了添加数组的最后一行;好的,这听起来很公平,但我不知道我的字典里有多少个对象,或者每个对象中有多少个图像。如何创建数量未知的数组?您希望eventImages
成为一个图像数组或图像数组?是的,我得到了您的想法,我刚刚用此行[eventImages addObject:[NSArray arrayWithArray:tempImageArray]]替换了添加数组的最后一行;我唯一能想到的另一种方法是使用一个包含所有图像的大数组,然后另一个数组只是图像数组的索引(这有意义吗)?我唯一能想到的另一种方法是使用一个包含所有图像的大数组,然后另一个数组只是图像数组的索引(这有意义吗)?
NSMutableArray *m1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableArray *m2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", nil];
for (id obj in m2)
[m1 addObject:obj];