【问题标题】:Argument type '[HKCategoryType?]' does not conform to expected type 'Hashable'参数类型 '[HKCategoryType?]' 不符合预期的类型 'Hashable'
【发布时间】:2016-09-23 17:54:29
【问题描述】:

我正在尝试使用代码为 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

但当我这样做时,我得到:

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

我如何在 Healthkit 中保存一个类别?一般来说,是否有专门针对 HKCategoryType 和 HKCategoryTypeIdentifierMindfulSession 的教程?

【问题讨论】:

    标签: ios swift3 healthkit


    【解决方案1】:

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

    您需要将Set<HKSampleType> 传递给requestAuthorization(toShare:read:)(该方法已在 Swift 3 中重命名),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)
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多