Recycled Steel
Recycled Steel

Reputation: 2272

Xcode Add NSMutableArray to a NSMutableArray

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

Upvotes: 1

Views: 1092

Answers (2)

aks
aks

Reputation: 109

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)

Upvotes: 0

Wain
Wain

Reputation: 119031

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];).

Upvotes: 4

Related Questions