Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 4从HealthKit获得首选单位_Ios_Swift_Healthkit - Fatal编程技术网

Ios Swift 4从HealthKit获得首选单位

Ios Swift 4从HealthKit获得首选单位,ios,swift,healthkit,Ios,Swift,Healthkit,我是Swift的初学者,我试图从HealthKit获得用户喜欢的装置 苹果为此提供了这个功能 func preferredUnits(for quantityTypes: Set<HKQuantityType>, completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void) func preferredUnits(对于quantityTypes:Set, 完成:@转义([HKQuantityT

我是Swift的初学者,我试图从HealthKit获得用户喜欢的装置

苹果为此提供了这个功能

func preferredUnits(for quantityTypes: Set<HKQuantityType>, 
     completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void)
func preferredUnits(对于quantityTypes:Set,
完成:@转义([HKQuantityType:HKUnit],错误?->无效)

但我真的不知道如何正确地实施它。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){

}
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意])->Bool{
//应用程序启动后自定义的覆盖点。
让bloodGlucose=HKObjectType.quantityType(forIdentifier:.bloodGlucose)
让HKQuantitySet:Set=[血糖!]
self.preferredUnits(对于:HKQuantitySet as!Set,完成:{(preferedUnit,error))在
打印(首选单位)
})
返回真值
}
func preferredUnits(对于quantityTypes:Set,
完成:@转义([HKQuantityType:HKUnit],错误?->无效){
}
我很确定我完全错了


有人知道如何正确地实现该功能吗?一般来说,不仅对于
bloodglucose
,这也很好。

您需要调用
preferredUnits
函数作为
HKHealthStore
的实例。您的代码正在调用一个您用相同签名声明的函数,该函数不起任何作用。

您需要将
preferredUnits
函数调用为
HKHealthStore
的实例。您的代码只是在调用一个您用相同的签名声明的函数,而该签名不起任何作用。

到目前为止,您的实现看起来还不错。您希望如何处理该值?您可能会将其存储在属性中,我会将其存储在userdefaults中。这不是问题所在。问题是我没有得到任何值或错误。应用程序只是不执行该功能。在获得权限状态后,我将其实现到应用程序委托文件中。您需要在上下文中显示更多代码。这个代码应该如何调用?必须有更多的代码。您可以在AppDeaveT的中间键入此代码。它必须包含在某个函数中。哪个功能?该函数如何调用?不要将附加信息作为答案发布;你的问题。到目前为止,你看起来还不错。您希望如何处理该值?您可能会将其存储在属性中,我会将其存储在userdefaults中。这不是问题所在。问题是我没有得到任何值或错误。应用程序只是不执行该功能。在获得权限状态后,我将其实现到应用程序委托文件中。您需要在上下文中显示更多代码。这个代码应该如何调用?必须有更多的代码。您可以在AppDeaveT的中间键入此代码。它必须包含在某个函数中。哪个功能?该函数如何调用?不要将附加信息作为答案发布;你的问题。