ios - 什么类型的 HealthKit 单元用于 VO2 Max 测量? - swift 4

标签 ios swift healthkit

我正在使用 Apple 的 HealthKit 使用 Swift 4 编写一个应用程序。我正在尝试写入“VO2 Max”指标,但我不确定要使用哪个单位。在 Health App 本身中,该单位列为“mL/(kg*min)”,但我在 Apple 的文档中没有看到类似的内容 here .我的代码看起来像这样。我应该用什么代替???在第二行?

writeHKMetric = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.vo2Max)!
writeHKQuantity = HKQuantity.init(unit: HKUnit.???, doubleValue: 1)

(作为引用,写入的代码,比方说,骑行距离看起来像这样。注意“HKUnit.mile()”)

writeHKMetric = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceCycling)!
writeHKQuantity = HKQuantity.init(unit: HKUnit.mile(), doubleValue: 1)

当我尝试使用另一个指标时,比如 count,我得到这个错误:

2017-12-14 22:08:01.876935-0500 Stand Hours 2.0[478:76005] *** Terminating app due to uncaught exception '_HKObjectValidationFailureException', reason: 'HKQuantitySample 1 count  (2017-12-08 08:00:00 -0500 - 2017-12-08 08:00:00 -0500) requires unit of type Volume/Mass·Time. Incompatible unit: count'

但我不知道如何将我的单位设置为“体积/质量*时间”。

最佳答案

单位的乐趣。 HKUnit 有乘除单位的方法。

以下代码构建所需的单元。

let kgmin = HKUnit.gramUnit(with: .kilo).unitMultiplied(by: .minute())
let mL = HKUnit.literUnit(with: .milli)
let VO₂Unit = mL.unitDivided(by: kgmin)

let writeHKMetric = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.vo2Max)!
let writeHKQuantity = HKQuantity(unit: VO₂Unit, doubleValue: 1)
print(writeHKQuantity)

输出:

1 mL/min·kg

创建单元的一个不那么有趣但更简单的解决方案(感谢 Allan)是:

let VO₂Unit = HKUnit(from: "ml/kg*min")

关于ios - 什么类型的 HealthKit 单元用于 VO2 Max 测量? - swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825244/

相关文章:

ios - 单元测试 UICollectionView.indexPath(: UICollectionViewCell)

swift - 如何从 WatchOS 中的 SwiftUI 列表中删除分隔符?

objective-c - 在 myproject-swift.h 中找不到接口(interface)声明

ios - HKActivitySummary dateComponents 落后一天

ios - 为什么我的 iPhone 无法从 Apple Watch 获取心率数据,但 watch 扩展可以?

ios - 如何使用 xib 文件在 ScrollView 中加载特定的 ViewController?

ios - 在 Swift 3.0 中类型转换

iPhone 应用程序加载器故障排除

Swift & SpriteKit - 使用 break 的 switch 语句

ios - 使用 HKWorkoutSession 时如何避免将 watch 应用程序放在前面?