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

Cannot convert value of type '[HKQuantityType?]' to specified type 'Set'

更新到 Swift 2

时出现此错误

Cannot convert value of type '[HKQuantityType?]' to specified type '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)
    }
}

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

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