Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am adding an NSMutableArray to another NSMutableArray, the problem is all the objects in the first array are the same (length size content, etc). I am guessing that when you add an array to an array the first array simple holds a pointer to a second, so how do I get it to hold a unique array? I am thinking I need to use arrayWithArray when adding but I can not figure out syntax.

My dictionary contains a number of Objects, each object has a load of image URLs which it then downloads.

My code thus far;

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];
}

The logs through this out (as you can see each image URL and size if different).

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

When I then cycle through them I get 4 copies of the last array (i.e. just 2 pics).

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

EDIT Thanks for help, nudge in right direction and changed last line to read;

[eventImages addObject:[NSArray arrayWithArray:tempImageArray]];
share|improve this question
    
The only other way I can think to do this is have one large array with all the images and then the other array is just an index to the image array (does that make sense)? –  Recycled Steel Jul 5 '13 at 13:04
add comment

2 Answers

up vote 4 down vote accepted

The problem is that you shouldn't be using removeAllObjects as it just cleans the array out (deleting the work you just did). Instead, you should be creating a new array (tempImageArray = [NSMutableArray array];).

share|improve this answer
    
Okay that sounds fair but I do not know how many objects will be in my dictionary OR how many images are in each object. How do I create an unknown number of arrays? –  Recycled Steel Jul 5 '13 at 13:02
    
You want eventImages to be an array of images or an array of arrays of images? –  Wain Jul 5 '13 at 13:04
    
Yep got your idea I just replaced last line which adds the array with this line [eventImages addObject:[NSArray arrayWithArray:tempImageArray]]; –  Recycled Steel Jul 5 '13 at 13:08
add comment

So you want to combine them? You can do it like this:

NSMutableArray *m1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableArray *m2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", nil];

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

(not sure if this was your problem)

share|improve this answer
    
This would work if I knew how many m1, m2.... to make. –  Recycled Steel Jul 5 '13 at 13:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.