Dev*_*Bär 11 core-motion swift
我收到CMLogItem来自 swift 的 CoreMotion 查询(可能是加速度计、陀螺仪)。现在,我想获取该样本的时间戳,最好是 Date() 对象。s 具有类型 的CMLogItem属性。.timestampTimeInterval
该文档告诉我以下内容:
CMLogItem 类定义了一个只读时间戳属性,用于记录进行运动事件测量的时间。
但是,我不确定如何将此时间戳转换为 Date() 对象,因为我不知道时间戳指的是什么。
另一个文档说:
时间戳是自设备启动以来的时间量(以秒为单位)。
但这看起来真的很奇怪,我不明白为什么苹果会创建如此不一致且复杂的 API。
正确答案是:
extension CMLogItem {
static let bootTime = Date(timeIntervalSinceNow: -ProcessInfo.processInfo.systemUptime)
func startTime() -> Date {
return CMLogItem.bootTime.addingTimeInterval(self.timestamp)
}
}
Run Code Online (Sandbox Code Playgroud)
这为我们提供了稳定、单调的结果,而每次调用 startTime 时都会计算 bootTime 时,情况就不是这样了。