ARC并在app中释放内存

时间:2013-12-14 00:13:04

标签: ios objective-c automatic-ref-counting

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

}

制作一个简单的水晶球应用程序,并尝试动画它。我真的一直在这工作一段时间没有运气。希望有人可以帮助我,因为我正在学习,尤其是ARC。 我知道我需要在viewDidLoad函数中添加一行来将引用计数添加到self.animationImagesArray的+1,因为当我转到self.background_image.animationImages self.animationImagesArray时,ARC已发布它和我知道因为在底部的调试器中,它的值为零。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

格雷迪是对的。你从来没有初始化数组。如果你没有像他发布的初学者(对于非常新的人来说,这可能令人困惑)只需这样做:

//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]];

}

答案 1 :(得分:0)

一般来说,你想要做的是一个坏主意...... ARC确实让你了解了这么简单的案例,你的问题是你没有将self.animationImagesArray初始化为任何东西。

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