tags:

views:

31

answers:

1

Hey Guys

In an effort to 'clean up' my code - I was wondering if this could be made simpler. I have 32 images and I was adding them like this

 [theCarPics addObject:[UIImage imageNamed:@"1.jpg"]];
 [theCarPics addObject:[UIImage imageNamed:@"2.jpg"]];
 //...
 [theCarPics addObject:[UIImage imageNamed:@"32.jpg"]];

is there a simpler way? loop perhaps?

Any ideas would be appreciated guys

Thanks

Sam

+1  A: 
for (int c=1; c<=kNumberOfCars; c++)
{
    NSString *fileName = [NSString stringWithFormat:@"%i.jpg", c];
    [theCarPics addObject:[UIImage imageNamed:fileName]];
}

Update: The kNumberOfCars is just a constant, of course. You can replace it by an actual value or (better) declare it somewhere at the top of your file:

static const int kNumberOfCars = 32;
zoul
thats just not working for some reason... ive changed it to this. for (int c = 1; c < 32; c++){ NSString *fileName = [NSString stringWithFormat:@"%i.jpg", c]; [theCarPics addObject:[UIImage imageNamed:fileName]]; }
Sam Jarman
`<=`, 32 is needed.
KennyTM
Oh yes, I forgot we are counting from one. Thanks, fixed.
zoul
ah... i figured it out - i hadn't init'd the array *d'uh*
Sam Jarman