0
votes

I wanted to add an simple picture animation in my app, but its not showing. I get this yellow error saying "local declaration of 'images' hides instance variable"

In the following place: [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]]; }

// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
animationImageView.animationImages = **images**;

MY CODE:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Load images
    NSArray *imageNames = @[@"1.png", @"2.png", @"3.png", @"4.png",
                            @"5.png", @"6.png"];

    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    // Normal Animation
    UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
    animationImageView.animationImages = images;
    animationImageView.animationDuration = 0.5;

    [self.view addSubview:animationImageView];
    [animationImageView startAnimating];

}
1
Always prefix instance variables with an underscore, and then you won't ever run into this problem.jlehr

1 Answers

1
votes

The warning means that the declaration of a local variable below

NSMutableArray *images = [[NSMutableArray alloc] init];
//              ^^^^^^

uses the name that you have used for an instance variable in the declaration of your class. Names of instance variables are listed in curly braces after the class declaration:

@interface MyController : UIViewController {
    NSMutableArray *images; // <<== The "competing" declaration
}
...
@end

You can rename one of these two variables to fix the problem, for example:

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

Another possibility is to get rid of one of these declarations. Make sure that you need both the local and the instance variables. If one of them is sufficient, removing the other one will fix the problem as well.