Objective c 最佳实践:如何在每次调用选择器方法后更新重复的NSTimer间隔

Objective c 最佳实践:如何在每次调用选择器方法后更新重复的NSTimer间隔,objective-c,xcode,cocoa,Objective C,Xcode,Cocoa,我有一个NSTimer对象需要重复执行。它调用的代码最终在我的应用程序中通过4种不同的状态切换设置。每次状态改变时,我都希望计时器上的间隔改变。此循环应在应用程序的整个生命周期内持续 据我所知,您无法更新NSTimer上的interval属性。我相信这意味着每次在计时器代码中切换状态时,我都需要添加代码来创建一个新的NSTimer对象。以下是我的想法: //Consider that in my containing class, there is an NSTimer property, na

我有一个
NSTimer
对象需要重复执行。它调用的代码最终在我的应用程序中通过4种不同的状态切换设置。每次状态改变时,我都希望计时器上的间隔改变。此循环应在应用程序的整个生命周期内持续

据我所知,您无法更新
NSTimer
上的
interval
属性。我相信这意味着每次在计时器代码中切换状态时,我都需要添加代码来创建一个新的
NSTimer
对象。以下是我的想法:

//Consider that in my containing class, there is an NSTimer property, named myTimer.

//My selector method, used by myTimer
-(void) updateState:(NSTimer *) timer
{
    switch (AppState)
    {
    case state_1:
        //Update current state to state_2
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:30.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    case state_2:
        //Update current state to state_3
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:5.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    case state_3:
        //Update current state to state_4
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:30.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    case state_4:
        //Update current state back to state_1
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:5.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    }
}
这是最理想的解决方案,还是创建所有这些计时器会在应用程序持续执行时给应用程序带来任何形式的麻烦

此外,我在我的项目中加入了ARC。在这种情况下,我是否仍需要销毁以前的
NSTimer
对象

当我的应用程序持续执行时,所有这些计时器的创建是否会给它带来任何形式的麻烦

只要你能正确管理内存就行。使用合成setter将为您解决此问题

在这种情况下,我是否仍需要销毁以前的
NSTimer
对象

不明确。当非重复计时器触发时,它会使自身失效,并且运行循环会发送
release
(因为它保留了它)。如果你对它有一个声明,你也需要释放它,你可以使用setter来释放它

由于这些计时器都不重复,因此您可以放弃ivar,改为使用
performSelect:withObject:afterDelay:

- (void)updateState {

    NSTimeInterval delay = 30;
    switch(AppState)
    {
        case 1: { break; }
        case 2: { delay = 5.0;
                  break; }
        case 3: { break; }
        case 4: { delay = 5.0;
                  break; }
    }

    [self performSelector:@selector(updateState) 
               withObject:nil 
               afterDelay:delay];

}

(顺便说一句,我假设你的真实代码中有
break
s。如果没有,你肯定应该这样做。)

如果这些是计时间隔,您最好使用一个重复的5秒计时器,并利用计数器在状态3和状态1中获得更长的间隔。@TriPhoenix我认为这可能值得一个答案。我认为我更喜欢这个答案而不是另一个答案。另外,是的,我的实际代码中有中断。我上面的代码完全是作为示例编写的。我可能会把这个问题留一天左右,看看是否还有其他可行的答案;你的程序没有问题,所以我的回答只是一个建议。