无法将'[HKQuantityType?]'类型的值转换为指定类型'Set'

时间:2015-09-29 14:29:06

标签: ios swift set

更新到Swift 2时出现此错误

  

无法将'[HKQuantityType?]'类型的值转换为指定类型'Set'

private let stepsCountIdentifier = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)


func authorizeHealthKit(completion: ((success: Bool, error: NSError?) -> Void)) {
    let healthKitTypesToRead: Set = [stepsCountIdentifier]

    if !HKHealthStore.isHealthDataAvailable() {
        completion(success: false, error: NSError(domain: "steps", code: -1, userInfo: nil))
        return
    }

    healthKitStore.requestAuthorizationToShareTypes(Set(), readTypes: healthKitTypesToRead) { (success, error) -> Void in
        completion(success: success, error: error)
    }
}

1 个答案:

答案 0 :(得分:2)

在初始化stepCountIdentifier集之前,您需要打开可选的healthKitTypesToRead

if let stepsCountIdentifierUnwrapped = stepsCountIdentifier {
    let healthKitTypesToRead: Set = [stepsCountIdentifierUnwrapped]
}