【发布时间】:2020-01-31 21:28:36
【问题描述】:
我想使用 HealthKit 中的静息心率功能 - 在此线程中定义:
Query to Healthstore for Resting Heart Rate not returning any values
func getuserRestingHeartRate(completion: @escaping (HKQuantitySample) -> Void) {
guard let restingHeartRateSampleType = HKSampleType.quantityType(forIdentifier: .restingHeartRate) else {
print("Resting Heart Rate Sample Type is no longer available in HealthKit")
return
}
//1. Use HKQuery to load the most recent samples.
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
end: Date(),
options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
//let limit = 1
let sampleQuery = HKSampleQuery(sampleType: restingHeartRateSampleType,
predicate: mostRecentPredicate,
limit: HKObjectQueryNoLimit,
sortDescriptors:
[sortDescriptor]) { (query, samples, error) in
DispatchQueue.main.async {
guard let samples = samples,
let mostRecentSample = samples.first as? HKQuantitySample else {
print("getUserRestingHeartRate sample is missing")
return
}
completion(mostRecentSample)
}
}
HKHealthStore().execute(sampleQuery)
}
但我似乎无法在 Swift (5.X) 中找出正确的语法!我猜我的技能水平以上......
试过这个:
var restingHeartRate: HKQuantitySample?
getuserRestingHeartRate(completion: (HKQuantitySample) -> (Void))
上面给出了这个错误:
Cannot convert value of type '((HKQuantitySample) -> (Void)).Type' to expected argument type '(HKQuantitySample) -> Void’
getuserRestingHeartRate(completion: (restingHeartRate) -> (Void))
上面给出了这个错误:Expected type before '->’
getuserRestingHeartRate(completion: (restingHeartRate))
上面给出了这个错误:Cannot convert value of type 'HKQuantitySample?' to expected argument type '(HKQuantitySample) -> Void’
【问题讨论】:
-
请格式化您的代码。使用提供的编辑工具,并阅读提示。
-
好的 - 进行了编辑!