参数类型“[HKCategoryType?]”不符合预期类型 'Hashable'

Argument type '[HKCategoryType?]' does not conform to expected type 'Hashable'

我正在尝试使用代码请求对 healthkit 中的类别的授权:

let healthKitStore: HKHealthStore = HKHealthStore()
let healthKitTypesToWrite = Set(arrayLiteral:[
    HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifierMindfulSession)
    ])
healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in

    if( completion != nil )
    {
      completion(success:success,error:error)
    }
}

来自 https://www.raywenderlich.com/86336/ios-8-healthkit-swift-getting-started

然而,当我这样做时,我得到:

Argument type '[HKCategoryType?]' does not conform to expected type 'Hashable'

我如何在 Healthkit 中保存类别?通常是否有专门针对 HKCategoryType 以及 HKCategoryTypeIdentifierMindfulSession 的教程?

链接的文章不是从 ArrayLiteral 创建 Set 的好例子。

需要传递一个Set<HKSampleType>requestAuthorization(toShare:read:)(方法在Swift3中已经重命名),Swift不擅长推断集合类型。

因此,您最好显式声明 healthKitTypesToWritehealthKitTypesToRead 的每种类型。

let healthKitTypesToWrite: Set<HKSampleType> = [
    HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!
]
let healthKitTypesToRead: Set<HKObjectType> = [
    //...
]
healthKitStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) -> Void in

    completion?(success, error)
}

通过为某些 Set 类型提供 ArrayLiteral,Swift 尝试将 ArrayLiteral 转换为 Set,内部调用 Set.init(arrayLiteral:)。你通常不需要直接使用Set.init(arrayLiteral:)