检查方法调用objective-c之间的时间差

时间:2012-12-09 20:10:20

标签: objective-c

我有这个方法:

- (void) receivedData {
}

这应该每50到100毫秒运行一次。有没有办法检查自上次调用以来的时间并将最后一次打印到NSString?

1 个答案:

答案 0 :(得分:1)

@interface MyClass() {
    NSDate* receivedDataTimestamp;
    ....
}

...

-(void)receivedData {
    NSDate* now = [NSDate date];
    if (receivedDataTimestamp != nil) {
        NSTimeInterval delta = [now timeIntervalSinceDate:receivedDataTimestamp];
        NSLog(@"%f seconds since recevedData was called last", delta);
    }
    receivedDataTimestamp = now;
    ....
}