我已成功写入 HealthKit,但收到这些值总是返回 0。我正在尝试返回最新的体重值。
这是我读取重量的功能:
public func readWeight(result: @escaping (Double) -> Void) {
print("Weight")
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
result(0.0)
}
guard let results = results else {
print("No results of query")
result(0.0)
return
}
if (results.count == 0) {
print("Zero samples")
result(0.0)
return
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
result(0.0)
return
}
result(bodymass.quantity.doubleValue(for: HKUnit.pound()))
}
healthKitStore.execute(weightQuery)
}
这就是我将其设置为变量的方式:
readWeight() { weight in
Weight = weight
}
谢谢!