首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode将NSMutableArray添加到NSMutableArray

Xcode将NSMutableArray添加到NSMutableArray
EN

Stack Overflow用户
提问于 2013-07-05 20:36:59
回答 2查看 1.1K关注 0票数 1

我正在向另一个NSMutableArray添加一个NSMutableArray,问题是第一个数组中的所有对象都是相同的(长度、大小、内容等)。我猜想当你将一个数组添加到一个数组中时,第一个数组简单地持有一个指向第二个数组的指针,那么我如何让它持有一个唯一的数组呢?我想我需要在添加的时候使用arrayWithArray,但是我不能理解语法。

我的NSDictionary包含许多对象,每个对象都加载了图像URL,然后下载这些URL。

到目前为止我的代码;

代码语言:javascript
复制
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和大小不同)。

代码语言:javascript
复制
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张照片)。

代码语言:javascript
复制
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

编辑感谢帮助,向正确的方向轻推,并将最后一行改为阅读;

代码语言:javascript
复制
[eventImages addObject:[NSArray arrayWithArray:tempImageArray]];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-05 20:44:40

问题是你不应该使用removeAllObjects,因为它只是清理了数组(删除了你刚刚做的工作)。相反,您应该创建一个新数组(tempImageArray = [NSMutableArray array];)。

票数 4
EN

Stack Overflow用户

发布于 2013-07-05 20:51:50

所以你想把它们组合起来?你可以这样做:

代码语言:javascript
复制
NSMutableArray *m1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableArray *m2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", nil];

for (id obj in m2)
    [m1 addObject:obj];

(不确定这是不是你的问题)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17489090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档