Access NSArray from a NSTimer Interval = EXC_BAD_ACCESS

六眼飞鱼酱① 提交于 2020-01-24 13:33:27

问题


I have some code that looks like this:

actualColor = 0;
targetColors = [NSArray arrayWithObjects:[UIColor blueColor],
                                         [UIColor purpleColor],
                                         [UIColor greenColor],
                                         [UIColor brownColor],
                                         [UIColor cyanColor], nil];
timer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                         target:self
                                       selector:@selector(switchScreen)
                                       userInfo:nil
                                        repeats:YES];

And in the selector i have this:

- (void) switchScreen
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];

    int totalItens = [targetColors count];
    NSLog(@"Total Colors: %i",totalItens);
    if(actualColor >= [targetColors count])
    {
        actualColor = 0;
    }

    UIColor *targetColor = [targetColors objectAtIndex:actualColor];

    if(!firstUsed)
    {
        [firstView setBackgroundColor:targetColor];
        [secondView setAlpha:0.0];
        [firstView setAlpha:1.0];
        firstUsed = YES;
    }
    else 
    {
        [firstView setBackgroundColor:targetColor];
        [secondView setAlpha:1.0];
        [firstView setAlpha:0.0];
        firstUsed = NO;
    }
    [UIView commitAnimations];

    actualColor++;        
}

But it seems that I cannot access my array inside the scheduledTimer Action! Have I perhaps missed something?


回答1:


arrayWithObjects: returns an autoreleased object, and since you're not retaining it it's being deallocated at the end of the run loop, before your timer fires. You want to either retain it or use the equivalent alloc/init method, and release it when you're done with it. Be sure to read about memory management first though, you're going to run into all kinds of problems like this until you have a good understanding of it.




回答2:


You will have to make the targetColors array and the actualColor variable into instance variables for your class so that they can be used in the timer method. It would look something like this:

@interface YourClass : NSObject
{
    //...
    int actualColor;
    NSArray * targetColors;
}
@end

@implementation YourClass

- (id)init
{
    if ((self = [super init]) == nil) { return nil; }

    //...
    actualColor = 0;
    targetColors = [[NSArray arrayWithObjects:[UIColor blueColor],
                                              [UIColor purpleColor],
                                              [UIColor greenColor],
                                              [UIColor brownColor],
                                              [UIColor cyanColor],
                                             nil] retain];
    return self;
}

- (void)dealloc
{
    [targetColors release];
    //...
    [super dealloc];
}

//...

@end


来源:https://stackoverflow.com/questions/1027658/access-nsarray-from-a-nstimer-interval-exc-bad-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!