gpt4 book ai didi

iphone - 从 NSTimer 访问 NSArray 间隔 = EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-12-03 16:25:03 24 4
gpt4 key购买 nike

我有一些代码,如下所示:

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

在选择器中我有这个:

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

但我似乎无法在 ScheduledTimer Action 中访问我的数组!我是不是错过了什么?

最佳答案

arrayWithObjects: 返回一个自动释放的对象,由于您没有保留它,因此它会在运行循环结束时、计时器触发之前被释放。您想要保留它或使用等效的 alloc/init 方法,并在完成后释放它。不过,请务必先阅读有关内存管理的内容,在充分理解它之前,您将遇到诸如此类的各种问题。

关于iphone - 从 NSTimer 访问 NSArray 间隔 = EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1027658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com