ios - Swift 4 从 HealthKit 获取 preferredUnit

标签 ios swift healthkit

我是 Swift 的初学者,我试图从 HealthKit 获取用户首选单位。

苹果为其提供了这个功能

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

Source

但是我真的不知道如何正确地实现它。 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/

相关文章:

ios - 是否应该为已发布的应用关闭 NSZombieEnabled?

ios - ARKit 有没有办法识别被触摸的节点?

Swift 4. 在继续执行之前等待 HealthKit HKQuery 的异步结果

android - 通过 FHIR 与医院集成 - 访问医院 FHIR 服务器

ios - 如何检查字符串所在的数字数组。使用swift

ios - 带有 HealthKit 的通用 iOS 应用程序未出现在 iPad App Store 中

ios - 从 Table View 中的 url 异步获取图像

ios - AppStore 更新后 STKeychain 找回密码失败

ios - UIColor 图案图像和色调颜色

ios - 在函数中使用闭包