我问了一个快速的问题here并收到了这个答案here,其中摆脱了错误,但现在我收到了一个新错误。
Binary operator '*' cannot be applied to operands of type '(@escaping (Double) -> Void) -> ()' and 'Double'
现在代码如下:
func readWeight(result: @escaping (Double) -> Void) {
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)
}
if (results.count == 0) {
print("Zero samples")
result(0.0)
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
result(0.0)
}
result(bodymass.quantity.doubleValue(for: HKUnit.pound()))
}
healthKitStore.execute(weightQuery)
}
每当我尝试将它乘以时,我就会收到此错误:
self.readWeight * 0.67
谢谢!
答案 0 :(得分:1)
函数readWeight
不返回任何内容,它使用闭包作为异步回调。
适当的语法是
readWeight() { weight in
let result = weight * 0.67
}