我是 Swift 的初学者,我试图从 HealthKit 获取用户首选单位。
苹果为其提供了这个功能
func preferredUnits(for quantityTypes: Set<HKQuantityType>,
completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void)
但是我真的不知道如何正确地实现它。 Atm 函数在我的应用程序中什么都不做。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let bloodGlucose = HKObjectType.quantityType(forIdentifier: .bloodGlucose)
let HKQuantitySet: Set<HKSampleType> = [bloodGlucose!]
self.preferredUnits(for: HKQuantitySet as! Set<HKQuantityType>, completion: { (preferedUnit, error) in
print(preferedUnit)
})
return true
}
func preferredUnits(for quantityTypes: Set<HKQuantityType>,
completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void){
}
我很确定我完全错了。
有人知道如何正确实现该功能吗?不仅对于血糖
,总的来说,会很好。
最佳答案
您需要将 preferredUnits
函数调用为 HKHealthStore
的实例。您的代码只是调用一个您已声明的具有相同签名但什么都不做的函数。
关于ios - Swift 4 从 HealthKit 获取 preferredUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50936871/