Swift 调用转义语法 closure/function...?
Swift syntax for calling an escaping closure/function...?
我想使用 HealthKit 中的静息心率函数 - 在此线程中定义:
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’
试试这个:
var restingHeartRate: HKQuantitySample?
getuserRestingHeartRate() { (sample) in
self.restingHeartRate = sample
}
或者,您可以使用:
var restingHeartRate: HKQuantitySample?
getuserRestingHeartRate(completion: { (sample) in
self.restingHeartRate = sample
})
这是对 Swift 闭包的一个很好的介绍:https://docs.swift.org/swift-book/LanguageGuide/Closures.html
我想使用 HealthKit 中的静息心率函数 - 在此线程中定义:
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’
试试这个:
var restingHeartRate: HKQuantitySample?
getuserRestingHeartRate() { (sample) in
self.restingHeartRate = sample
}
或者,您可以使用:
var restingHeartRate: HKQuantitySample?
getuserRestingHeartRate(completion: { (sample) in
self.restingHeartRate = sample
})
这是对 Swift 闭包的一个很好的介绍:https://docs.swift.org/swift-book/LanguageGuide/Closures.html