Swift-如何从healthStore.preferredUnits ..中提取HKUnit?

问题描述 投票:0回答:1

我正在使用Apple的HealthKit,并已正确授权所有内容并在我自己的语言环境(美国)中工作。

但是,我想为“ distanceWalkingRunning”获取用户的首选单位(HKUnit设置)。在我的情况下,这是“ mi”(英里)。对于其他世界地区,当然可以是公里,等等。

Apple的HealthStore提供此功能:

func preferredUnits(for quantityTypes: Set<HKQuantityType>, 
     completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void)

当我在下面的代码中使用此功能时:

var usrPreferredDistUnits: HKUnit?        
let quantityType : Set = [HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!]
healthStore.preferredUnits(for: quantityType) { (preferredType, error) in
   //self.usrPreferredDistUnits = preferredType; // error! how to extract HKUnit???
   print("\(preferredType)") }

我得到以下打印结果:

[HKQuantityTypeIdentifierDistanceWalkingRunning: mi]

这对于我的语言环境是正确的(Miles为“ mi”-因此我的代码可以正常工作。

但是,我无法弄清楚仅将返回值对的HKUnit部分(= mi)提取到变量“ var preferredDistanceUnit:HKUnit?”中的语法。

有人可以为我提供那一行代码的正确语法吗??

谢谢!

swift syntax syntax-error healthkit
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.