简体   繁体   English

迁移到Xcode 8和Swift 3.0时,Xcode Healthkit授权函数错误

[英]Xcode Healthkit authorized function error when migrate to Xcode 8 and Swift 3.0

The following function was working in Xcode 7.3 and Swift 2.*, after migrate to Xcode 8 and Swift 3.0 I got the following error when I try to compile 以下函数在Xcode 7.3和Swift 2. *中工作,迁移到Xcode 8和Swift 3.0后,当我尝试编译时出现以下错误

cannot call value of non-function type ((Bool, _ error:NSError?) -> Void)! 不能调用非函数类型的值((Bool, _ error:NSError?) -> Void)!

     func authorizeHealthKit(_ completion: ((_ success:Bool, _ error:NSError?) -> Void)!) {
            healthKitStore = HKHealthStore()
            let typesSet:Set<HKSampleType> = [
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!,
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.respiratoryRate)!,
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic)!,
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic)!, 
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!, 
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
                    HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.oxygenSaturation)!]


                healthKitStore?.requestAuthorization(toShare: typesSet, read: typesSet, completion: { (boolVal, error) in
                    completion(success: boolVal, error: error)   //<-- compile error here
            })
     }

Try making your completion handler optional as below: 尝试使您的完成处理程序可选如下:

completion?(success: boolVal, error: error) 完成?(成功:boolVal,错误:错误)

Also to note from Apple API documentation: 另请注意Apple API文档:

Important An iOS app linked on or after iOS 10.0 must include in its Info.plist file the usage description keys for the types of data it needs to access or it will crash. 重要信息iOS 10.0上或之后链接的iOS应用程序必须在其Info.plist文件中包含其需要访问的数据类型的使用说明密钥,否则将崩溃。 To access and update HealthKit data specifically, it must include the NSHealthShareUsageDescription and NSHealthUpdateUsageDescription keys, respectively. 要专门访问和更新HealthKit数据,它必须分别包含NSHealthShareUsageDescription和NSHealthUpdateUsageDescription键。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM