Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Images Based Animation - Populating the images via looping?

```self.backgroundImageView.animationImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"CB00001"], [UIImage imageNamed:@"CB00002"], [UIImage imageNamed:@"CB00003"], [UIImage imageNamed:@"CB00004"], [UIImage imageNamed:@"CB00005"], [UIImage imageNamed:@"CB00006"], [UIImage imageNamed:@"CB00007"], [UIImage imageNamed:@"CB00008"], [UIImage imageNamed:@"CB00009"], [UIImage imageNamed:@"CB00010"], [UIImage imageNamed:@"CB00011"], [UIImage imageNamed:@"CB00012"], [UIImage imageNamed:@"CB00013"], [UIImage imageNamed:@"CB00014"], [UIImage imageNamed:@"CB00015"], [UIImage imageNamed:@"CB00016"], [UIImage imageNamed:@"CB00017"], [UIImage imageNamed:@"CB00018"], [UIImage imageNamed:@"CB00019"], [UIImage imageNamed:@"CB00020"], [UIImage imageNamed:@"CB00021"], [UIImage imageNamed:@"CB00022"], [UIImage imageNamed:@"CB00023"], [UIImage imageNamed:@"CB00024"], [UIImage imageNamed:@"CB00025"], [UIImage imageNamed:@"CB00026"], [UIImage imageNamed:@"CB00027"], [UIImage imageNamed:@"CB00028"], [UIImage imageNamed:@"CB00029"], [UIImage imageNamed:@"CB00030"], [UIImage imageNamed:@"CB00031"], [UIImage imageNamed:@"CB00032"], [UIImage imageNamed:@"CB00033"], [UIImage imageNamed:@"CB00034"], [UIImage imageNamed:@"CB00035"], [UIImage imageNamed:@"CB00036"], [UIImage imageNamed:@"CB00037"], [UIImage imageNamed:@"CB00038"], [UIImage imageNamed:@"CB00039"], [UIImage imageNamed:@"CB00040"], [UIImage imageNamed:@"CB00041"], [UIImage imageNamed:@"CB00042"], [UIImage imageNamed:@"CB00043"], [UIImage imageNamed:@"CB00044"], [UIImage imageNamed:@"CB00045"], [UIImage imageNamed:@"CB00046"], [UIImage imageNamed:@"CB00047"], [UIImage imageNamed:@"CB00048"], [UIImage imageNamed:@"CB00049"], [UIImage imageNamed:@"CB00050"], [UIImage imageNamed:@"CB00051"], [UIImage imageNamed:@"CB00052"], [UIImage imageNamed:@"CB00053"], [UIImage imageNamed:@"CB00054"], [UIImage imageNamed:@"CB00055"], [UIImage imageNamed:@"CB00056"], [UIImage imageNamed:@"CB00057"], [UIImage imageNamed:@"CB00058"], [UIImage imageNamed:@"CB00059"], [UIImage imageNamed:@"CB00060"], nil];

Is the code. How could this be simplified via looping and adding to the array one at a time rather than this massive manually typed init?

```for (int i = 0; i<60; i++){
//what goes here
}

1 Answer

Hi Christopher,

You can use the code below in order to populate your array. Please keep in mind that it has to be a NSMutableArray in order to be able to add to it.

self.backgroundImageView.animationImages = [NSMutableArray array];
for (int i = 0; i < 60; i++) {
    [self.backgroundImageView.animationImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"CB%05d", i]]];
}

The trick is to use the %05d string formatter making the string to be formed of 5 numbers and have leading 0 if the number is not high enough.

Hope this helps.

Thank you: I've tried that code but am getting an error.

No visible @interface for 'NSArray' declares the selector 'addObject:'

Also, once this for loop is run, I still have to handle nil, is that done like this? ``` [self.backgroundImageView.animationImages addObject:nil]

As I said, your array has to be NSMutableArray and not NSArray since you can't add objects into an NSArray after initialization. You won't need to add the nil at the end if you are adding them dynamically.

Correct - here is a screenshot of the code with the error I'm receiving, very confusing!:

http://i.imgur.com/oPSGYx2.png

Let me know if I should paste more of the code, but it seemed fairly isolated! Thank you again

Sorry Christopher,

It slipped my mind that you were referring to the animationImages property of an UIImage. That property is a NSArray and can't be defined as a NSMutableArray. ( @property(nonatomic, copy) NSArray *animationImages - Apple definition).

To overcome this problem first define a NSMutableArray, load it up with the UIImage objects and at the end assign it to the animationImages property of the UIImage. Sorry for the mixup.

NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < 60; i++) {
    [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"CB%05d", i]]];
}
self.backgroundImageView.animationImages = (NSArray *)images;

This should fix your error.