我试图在两个时间点之间获得时间。我不知道我做错了什么,但我的 NSLog 给了我“nan”的结果。总时间为:纳秒。如果我从 if 语句中删除 [timer start] 和 [timer stop],那么它会给我双倍的正常结果。
- (void) filterFrequency {
Timer *timer = [[Timer alloc] init];
if (self.currentFrequency > 2000 && pointInTime % 2 == 0)
{
[timer startTimer];
pointInTime = pointInTime + 1;
}
else if (self.currentFrequency > 2000 && pointInTime % 2 == 1)
{
[timer stopTimer];
pointInTime = pointInTime + 1;
NSLog(@"Total time was: %f seconds", [timer timeElapsedInSeconds]);
}
这是 Timer.h #import
@interface Timer : NSObject {
NSDate *start;
NSDate *end;
}
- (void) startTimer;
- (void) stopTimer;
- (double) timeElapsedInSeconds;
- (double) timeElapsedInMilliseconds;
- (double) timeElapsedInMinutes;
@end
定时器.m
#import "Timer.h"
#import "ListenerViewController.h"
@implementation Timer
- (id) init {
self = [super init];
if (self != nil) {
start = nil;
end = nil;
}
return self;
}
- (void) startTimer {
start = [NSDate date];
}
- (void) stopTimer {
end = [NSDate date];
}
- (double) timeElapsedInSeconds {
return [end timeIntervalSinceDate:start];
}
- (double) timeElapsedInMilliseconds {
return [self timeElapsedInSeconds] * 1000.0f;
}
- (double) timeElapsedInMinutes {
return [self timeElapsedInSeconds] / 60.0f;
}
@end