开发者

Easier way to add lots of images to an array

开发者 https://www.devze.com 2022-12-26 11:27 出处:网络
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开发者_Go百科 them like this

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开发者_Go百科 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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号