2016-09-23 77 views
1

我试图通过代码来请求授权的范畴中healthkit:参数类型“[HKCategoryType?]”不符合预期型“哈希的”

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?

回答

5

链接的文章不是从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类型,斯威夫特尝试将ArrayLiteral转换为Set,在内部调用Set.init(arrayLiteral:)。您通常不需要直接使用Set.init(arrayLiteral:)

相关问题