1

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]];
1
  • 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)? Commented Jul 5, 2013 at 13:04

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
You want eventImages to be an array of images or an array of arrays of images?
Yep got your idea I just replaced last line which adds the array with this line [eventImages addObject:[NSArray arrayWithArray:tempImageArray]];
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.