1

I'm trying to loop through my mutable array and add image objects 3-81. Heres my code. Can someone tell me why its not working?

- (instancetype)init {
    self = [super init];
    if (self) {
        self.imageData = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"image1.gif"],       [UIImage imageNamed:@"image2.gif"],  nil];

        for (int i = 3; i <= 81; i++) {
            NSString *imageName = [NSString stringWithFormat:@"image%i", i];
            [self.imageData addObject:[UIImage imageNamed:imageName]];
        }
    }
    return self;
}
3
  • 1
    How exactly it is not working? Commented Sep 24, 2014 at 14:59
  • 2
    You should really be checking that the image exists before adding it to the array. If the image fails to load you'll be adding nil to an array which will throw an exception. Commented Sep 24, 2014 at 15:05
  • ur right. I'm new to programming. thanks for the tip Commented Sep 24, 2014 at 15:06

1 Answer 1

3

You are adding images named "image1.gif" and "image2.gif". However, in your loop you are adding images named "image3", "image4", "image5", etc. The missing ".gif" is likely the problem.

NSString *imageName = [NSString stringWithFormat:@"image%i.gif", i];
Sign up to request clarification or add additional context in comments.

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.