在没有更多上下文的情况下使表达式类型不明确

Getting Expression Type ambiguous without more context

我正在尝试申请使用健康应用程序数据的授权。

我收到以下错误:

FirstViewController.swift:44:9: Expression type '(typesToShare: _, readTypes: Set<HKSampleType>?, completion: (_, _) -> Void)' (aka '(typesToShare: _, readTypes: Optional<Set<HKSampleType>>, completion: (_, _) -> ())') is ambiguous without more context

在 44:9 其中 typesToShare: nil

let healthStore: HKHealthStore? = {
        if HKHealthStore.isHealthDataAvailable() {
            return HKHealthStore()
        } else {
            return nil
        }
    }()

    let dateOfBirthCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierDateOfBirth)

    let biologicalSexCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierBiologicalSex)

    let bloodTypeCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierBloodType)

    let dataTypesToRead: Set<HKSampleType>? = NSSet(objects:
        dateOfBirthCharacteristic!, biologicalSexCharacteristic!, bloodTypeCharacteristic!) as? Set<HKSampleType>

    let dataTypesToShare: Set<HKSampleType>? = NSSet() as? Set<HKSampleType>

    // Making the request
    let healthData = healthStore?.requestAuthorizationToShareTypes
    (typesToShare: nil,
     readTypes: dataTypesToRead,
     completion: { (success, error) -> Void in
                                            if success {
                                                println("success")
                                            } else {
                                                println(error.description)
                                            }
    })

我是 iOS 的新手,之前没有使用过 HealthKit。我该如何解决?

我也是 healthKit 的新手,但请尝试在 typesToShare 中的 nil 位置使用 dataTypesToShare 变量 argument.and 更好地创建 dataTypesToShare,就像您对 dataTypesToRead 所做的那样。而且这个函数没有 return 任何东西。你可以尝试使用闭包来获得完成块成功 .

      func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!)
{   
healthStore?.requestAuthorizationToShareTypes
    (typesToShare: nil,
     readTypes: dataTypesToRead,
     completion: { (success, error) -> Void in
                                            if success {
                                                println("success")
                                            } else {
                                            println(error.description)
                                        }
})
}