1

I'm trying to store my images in an array named "_images" but if I use NSLog() to view the data stored in image array, I get only one image. Would you guys help me out? Here's my code:

_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo1.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo2.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo3.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo4.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo5.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo6.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo7.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo8.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo9.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo10.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo11.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo12.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo13.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo14.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo15.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo16.png"]];

NSLog(@"ha ha ha:%d",_images.count);
3
  • 1
    This re-creates the array each time! Commented Jun 3, 2011 at 6:15
  • 6
    Please try to use reasonable grammar and spelling when you post questions. Questions that look like they were written by the Chik-fil-A cows are harder to read, less likely to be taken seriously, and less likely to get gud ansers. Commented Jun 3, 2011 at 6:27
  • especially from vegetarians. or chickens. or vegetarian chickens. Commented Jun 3, 2011 at 6:29

2 Answers 2

9

You're creating a new array on each line; you lose the reference to the old array and thus the image within it. You want to add new images to an existing array. Change lines 2 and onwards to the following:

[_images addObject:[UIImage imageNamed:...]];
Sign up to request clarification or add additional context in comments.

1 Comment

did u get the array description @kingston
7

You can also use a loop:

_images = [[[NSMutableArray alloc] init] autorelease];
for (int i=1; i<=16; ++i) {
  [_images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"logo%d.png",i]]];
}

1 Comment

Note that you can use _images = [NSMutableArray array] instead.

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.