iPhone 时间间隔自日期
我正在尝试测量点击之间的时间。我从其他几篇文章中收集了代码,但仍然没有运气。当我获取两个时间戳之间的差异并试图使其在几秒钟内可读时,我遇到了问题。
// Time elapsed
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:touchesBegan];
NSDateFormatter *formatted = [[NSDateFormatter alloc] init];
[formatted setDateFormat: @"ss"];
NSString *stringFromDate = [ formatted stringFromDate:duration];
错误出现在应该发生格式化的最后一行: 错误:“stringFromDate”的参数 1 的类型不兼容,
谢谢
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 stamps 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSDateFormatter
的stringFromDate:
方法采用NSDate
作为其参数。您正在向它传递一个NSTimeInterval
。NSTimeInterval
是一个双精度值,包含以秒为单位的持续时间。因此,您可以摆脱NSDateFormatter
并简单地使用持续时间(如双精度)生成一个字符串:The
stringFromDate:
method ofNSDateFormatter
takes anNSDate
as its argument. You are passing it anNSTimeInterval
.NSTimeInterval
is a double, containing a duration in seconds. So, you can get rid of theNSDateFormatter
and simply generate a string using the duration like a double: