iPhone 时间间隔自日期

发布于 2024-09-29 07:53:28 字数 437 浏览 0 评论 0原文

我正在尝试测量点击之间的时间。我从其他几篇文章中收集了代码,但仍然没有运气。当我获取两个时间戳之间的差异并试图使其在几秒钟内可读时,我遇到了问题。

    // 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2024-10-06 07:53:28

NSDateFormatterstringFromDate: 方法采用 NSDate 作为其参数。您正在向它传递一个 NSTimeIntervalNSTimeInterval 是一个双精度值,包含以秒为单位的持续时间。因此,您可以摆脱 NSDateFormatter 并简单地使用持续时间(如双精度)生成一个字符串:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文