开发者

iPhone timeIntervalSinceDate

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜