gpt4 book ai didi

ios - 提取每秒抽动的分钟形式 NSTimer

转载 作者:行者123 更新时间:2023-11-29 00:07:53 25 4
gpt4 key购买 nike

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myMethod) userInfo:nil repeats:YES];

-(void)myMethod {
//my every second requirment

//my every minute requirment
}

我的计时器每秒都在抽动,这是我的要求,但我还有另一个要求,例如,当我的应用程序在上午 10:05:30 启动时,我想在 10:06:00 时触发一些代码,并在 10:00 时再次触发: 07:00 继续。

我想这样做

date1 = dateFormatter ... date // this will give 10:05:30

date2 = dateFormatter ... date // this will give 10:05:00
date2 = addition of minute // this will give 10:06:00

and finally date1 compare date2 == descending // means it currently 10:06:00

但对我来说这不是一个好的解决方案,有没有更好的解决方案?

最佳答案

此解决方案效率更高,但请考虑 a tolerance of the NSTimer

@interface ViewController ()
@property (nonatomic, assign) NSInteger timerMinuteAddition;
@end

@implementation ViewController

- (void)yourScope {
// Here you compute the initial addition to the full minute
NSDate *currentDate = [NSDate new];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitSecond fromDate:currentDate];
self.timerMinuteAddition = 60 - [components second];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myMethod) userInfo:nil repeats:YES];
}

- (void)myMethod {
// your every second requirment

self.timerMinuteAddition--;
if (self.timerMinuteAddition == 0) {
// your every minute requirment

self.timerMinuteAddition = 60;
}
}

关于ios - 提取每秒抽动的分钟形式 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47494753/

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