如何在健康套件授权中添加第二个HKQuantityType? 我想添加心率和燃烧的能量,作为QuantityTypes的数组。当我在这行中添加这两种类型时let dataTypes = Set(arrayLiteral:quantityTypes)'而不是数量类型,作为数组,我收到错误"无法将HKQuantityType的值转换为预期的参数类型[]
我是初学者,我想我没有正确格式化数组。
guard let heartRateQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned) else {
// displayNotAllowed()
return
}
guard let calorieQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned) else {
// displayNotAllowed()
return
}
let quantityTypes = [calorieQuantityType, heartRateQuantityType]
let dataTypes = Set(arrayLiteral: quantityTypes)
healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypes) { (success, error) -> Void in
if success == false {
// self.displayNotAllowed()
}
}
答案 0 :(得分:0)
您正在将数组设置为Set然后请求授权。而是直接将calorieQuantityType和heartRateQuantityType添加到SET
//delete this let quantityTypes = [calorieQuantityType, heartRateQuantityType]
let dataTypes = Set([calorieQuantityType, heartRateQuantityType])
healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypes) { (success, error) -> Void in
if success == false {
// self.displayNotAllowed()
}
}