user2934028
user2934028

Reputation:

ARC and releasing memory in app

- (void)viewDidLoad
{

    [super viewDidLoad];

    self.predictionsObjectArray = [[AAPredictions alloc] init];

    [self.predictionsObjectArray setPredictionsArray:@[@"Probably Not", @"Ask Again", @"I doubt it", @"Unlikely", @"I believe so"]];



    for (int x = 1; x<61; x++) {
        NSMutableString *imageName = [[NSMutableString alloc] init];
        if (x > 9) {
            imageName = [NSMutableString stringWithFormat:@"CB000%i.png", x];
        }
        else {
           imageName = [NSMutableString stringWithFormat:@"CB0000%i.png", x];
        }

        [self.animationImagesArray addObject:[UIImage imageNamed:imageName]];

    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark Prediction

-(void)makePrediction {
    self.predictionLabel.text = [self.predictionsObjectArray getPrediction];
    [self animateItems];


}
-(void)animateItems {

    self.image_button.alpha = 0.0;

    self.background_image.animationRepeatCount = 1.0;

    self.background_image.animationImages = self.animationImagesArray;

    self.background_image.animationDuration = 3.0;
    [self.background_image startAnimating];






    while (self.background_image.isAnimating) {

    }

    self.image_button.alpha = 1.0;

}

Making a simple crystal Ball app, and am trying to animate it. I have really been working at this for a while with no luck. Hoping someone can help me as I am learning, particularly the ARC. I know I need to add a line in the viewDidLoad function to add the reference count to +1 of self.animationImagesArray, because when I goto assign self.background_image.animationImages to self.animationImagesArray, ARC released it and I know because in the debugger at the bottom, it’s value in nil. Any help would be great.

Upvotes: 0

Views: 71

Answers (2)

random
random

Reputation: 8608

Grady is correct. You never initialized the array. If you don't have an init like he posted (which for someone that is extremely new it maybe confusing) just do this:

//NEW: Initialize the array before adding items to it
self.animationImagesArray = [NSMutableArray new];

for (int x = 1; x<61; x++) {
    NSMutableString *imageName = [[NSMutableString alloc] init];
    if (x > 9) {
        imageName = [NSMutableString stringWithFormat:@"CB000%i.png", x];
    }
    else {
        imageName = [NSMutableString stringWithFormat:@"CB0000%i.png", x];
    }

    [self.animationImagesArray addObject:[UIImage imageNamed:imageName]];

}

Upvotes: 1

Grady Player
Grady Player

Reputation: 14549

generally what you are trying to do is a bad idea... ARC really has you covered in such a trivial case, your problem is that you don't initialize self.animationImagesArray to anything.

-(instancetype)init
{
    self = [super init]
    if (self)
    {
          self.animationImagesArray = [NSMutableArray new];
    }
    return self;
}

Upvotes: 0

Related Questions