开发者

iPhone timeIntervalSinceDate

开发者 https://www.devze.com 2023-01-21 06:29 出处:网络
I\'m trying to measure time between taps.I\'ve gathered code from a couple other posts, but still no luck.I running into issues when I\'m taking the difference between two time stam开发者_Python百科ps

I'm trying to measure time between taps. I've gathered code from a couple other posts, but still no luck. I running into issues when I'm taking the difference between two time stam开发者_Python百科ps and trying to make it readable in seconds.

    // Time elapsed
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:touchesBegan];

NSDateFormatter *formatted = [[NSDateFormatter alloc] init];
[formatted setDateFormat: @"ss"];

NSString *stringFromDate = [ formatted stringFromDate:duration];

The error comes on the last line where the formatting is supposed to occur: Error: Incompatible type for argument 1 of 'stringFromDate'

Thanks


The stringFromDate: method of NSDateFormatter takes an NSDate as its argument. You are passing it an NSTimeInterval. NSTimeInterval is a double, containing a duration in seconds. So, you can get rid of the NSDateFormatter and simply generate a string using the duration like a double:

NSString *stringFromInterval = [NSString stringWithFormat:@"%g", duration];
0

精彩评论

暂无评论...
验证码 换一张
取 消