【发布时间】:2023-04-08 23:18:01
【问题描述】:
代码:因此,当计时器 id 停止并重新启动几次时,此代码运行良好,但是当我们重复相同的次数超过 30 次时会崩溃。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
aTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(ShowTime:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run];});
更新标签
-(void)ShowTime:(NSTimer *) timer{
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
double Timeinterval= (now - StartClickTime)/60 ;
double wait=([FrequencyPerHour doubleValue]+[PromptTime doubleValue]+2.75);
double warningTime=([FrequencyPerHour doubleValue]+[WarningTime doubleValue]);
if (WarninigBeep == NO) {
if ((Timeinterval*60) >= warningTime)
{
// NSString *path = [NSString stringWithFormat:@"%@/rapidbeep9.mp3", [[NSBundle mainBundle] resourcePath]];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"rapidbeep9"
ofType:@"mp3"
inDirectory:nil];
NSURL *soundUrl = [NSURL fileURLWithPath:filePath];
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer play];
WarninigBeep=YES;
}
}
[lbltimerDisplay setText:[NSString stringWithFormat:@"%.4f", Timeinterval]];}
【问题讨论】:
-
[CFString 长度]:消息发送到已释放的实例 - 出现此错误
-
添加您的代码以便我们检查。
-
我正在使用这个代码 -(void)viewDidDisappear:(BOOL)animated{ [aTimer invalidate]; aTimer = nil; [超级 viewDidDisappear:YES]; }
-
基本上应用程序从数据库中获取之前停止的记录并在'showtimer'中进行计算并在标签上显示结果
标签: objective-c xcode memory-management memory-leaks