【问题标题】:Cannot convert value of type '[HKQuantityType?]' to specified type 'Set'无法将类型“[HKQuantityType?]”的值转换为指定类型“Set”
【发布时间】:2015-09-29 14:29:05
【问题描述】:

更新到 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)
    }
}

【问题讨论】:

    标签: ios swift set


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-27
      • 2017-03-17
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多