二进制运算符'*'不能应用于'(@escaping(Double) - > Void)类型的操作数 - > ()'和'双'?

时间:2017-06-22 14:46:03

标签: swift swift3

我问了一个快速的问题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

谢谢!

1 个答案:

答案 0 :(得分:1)

函数readWeight不返回任何内容,它使用闭包作为异步回调。

适当的语法是

readWeight() { weight in
    let result = weight * 0.67
}