无法使用类型参数列表调用“requestAuthorizationToShareTypes”

Posted

技术标签:

【中文标题】无法使用类型参数列表调用“requestAuthorizationToShareTypes”【英文标题】:Cannot invoke 'requestAuthorizationToShareTypes' with an argument list of type 【发布时间】:2015-08-05 15:14:10 【问题描述】:

上面写着:

Cannot invoke 'requestAuthorizationToShareTypes' with an argument list of type (HKQuantityType, readTypes: HKCharacteristicType, completion: (Bool, NSError!) -> Void)

请帮忙

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if HKHealthStore.isHealthDataAvailable() 
        let healthStore = HKHealthStore()
        healthStore.requestAuthorizationToShareTypes(HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!, readTypes: (HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex))! , completion:
            (success:Bool,error:NSError!) -> Void in
            if !success
                print("error")
            
        )
    


【问题讨论】:

【参考方案1】:

requestAuthorizationForTypes 方法需要 SetsHKObjectType。试试这个:

let shareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
let readTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]
healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes:readTypes, completion:  (success, error) -> Void in
    if !success
        print("error")
    
)

【讨论】:

【参考方案2】:

试试这个方法

// MARK: - HealthKit
func performAutorizationForHealthKit(var completion:((success: Bool, error: NSError!) -> Void)?) 
    let healthKitTypesToShare: Set = [ HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
    if !HKHealthStore.isHealthDataAvailable() 
        let error = NSError(domain: "com.mutualCore.healthKit", code: 1, userInfo: [NSLocalizedDescriptionKey : "HealthKit is not available in this Device"])
        completion?(success: false, error: error)
        completion = nil
        return;
    

    healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToShare, readTypes: [], completion: 
        (successRequest, errorRequest) -> Void in
        completion?(success: successRequest, error: errorRequest)
    )

用法

    performAutorizationForHealthKit  (success, error) -> Void in
        if error != nil 
            print("success")
        
    

也可以开始RayWenderlich tutorial HealthKit

【讨论】:

以上是关于无法使用类型参数列表调用“requestAuthorizationToShareTypes”的主要内容,如果未能解决你的问题,请参考以下文章

无法使用类型参数列表调用“requestAuthorizationToShareTypes”

无法使用“(nil)”类型的参数列表调用“保存”

无法使用类型为“”的参数列表调用类型“”的初始化程序

无法使用类型为“()”的参数列表为类型“Int”调用初始化程序

无法使用类型为“(字符串)”的参数列表调用“执行选择器”

无法使用类型参数列表调用“responseArray”