Swift给出非无效返回错误,即使它设置为双倍?

Swift给出非无效返回错误,即使它设置为双倍?,swift,function,swift3,return,double,Swift,Function,Swift3,Return,Double,我得到这个错误: /Users/xxxx/Desktop/xxx/xxx/ViewController.swift:43:38: Unexpected non-void return value in void function 还有一些函数是这样的,尽管我的函数设置为返回双精度 这是每个返回中都会出现此错误的函数之一 func readWeight() -> Double { let quantityType = HKQuantityType.quantityType(forI

我得到这个错误:

/Users/xxxx/Desktop/xxx/xxx/ViewController.swift:43:38: Unexpected non-void return value in void function
还有一些函数是这样的,尽管我的函数设置为返回双精度

这是每个返回中都会出现此错误的函数之一

func readWeight() -> Double {
    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!)
            return 0.0
        }

        guard let results = results else {
            print("No results of query")
            return 0.0
        }

        if (results.count == 0) {
            print("Zero samples")
            return 0.0
        }

        guard let bodymass = results[0] as? HKQuantitySample else {
            print("Type problem with weight")
            return 0.0
        }
        return bodymass.quantity.doubleValue(for: HKUnit.pound())
    }

    healthKitStore.execute(weightQuery)
}
如何使用的示例如下:

print(readWeight())

谢谢

必须在函数末尾返回双精度值

 func readWeight() -> Double {
 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!)
        return 0.0
    }

    guard let results = results else {
        print("No results of query")
        return 0.0
    }

    if (results.count == 0) {
        print("Zero samples")
        return 0.0
    }

    guard let bodymass = results[0] as? HKQuantitySample else {
        print("Type problem with weight")
        return 0.0
    }
    return bodymass.quantity.doubleValue(for: HKUnit.pound())
}

healthKitStore.execute(weightQuery)
return some double value here
}

我想你想要达到的是这样的目标:

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)
}
readWeight(result: { myResult in
    let comparisonResult = myResult == 0.0
})
然后您可以使用如下方法使用
readWeight

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)
}
readWeight(result: { myResult in
    let comparisonResult = myResult == 0.0
})
因此,
myResult
这里是您要查找的值

这都是由于
HKSampleQuery
异步执行,因此无法立即知道返回值。您必须等待其结果,然后在作为方法参数给出的闭包中处理结果

也许您应该阅读更多关于闭包的内容:


以下部件末端的支架内的线条:

let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) { ... }
实际上是另一个闭包的一部分,作为参数发送给
HKSampleQuery
的初始值设定项。将它们视为一个单独的函数,在调用
healthKitStore.execute(weightQuery)
时将执行该函数。正如该闭包的签名所述:
(HKSampleQuery,[HKSample]?,Error?->Void
。这意味着您不能从该闭包内的语句返回任何内容

记住这一点,您可以按如下方式修改您的方法:

func printWeight() {
    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!)
        }

        guard let results = results else {
            print("No results of query")
        }

        if (results.count == 0) {
            print("Zero samples")
        }

        guard let bodymass = results[0] as? HKQuantitySample else {
            print("Type problem with weight")
        }

        print("Bodymass:",  bodymass.quantity.doubleValue(for: HKUnit.pound()))
    }

    healthKitStore.execute(weightQuery)
}

然后只需调用
printwweight()
即可获得结果。

您需要使用block。因此函数本身将返回void。当
HKSampleQuery
启动时,它将被执行并等待结果,而
readWeight
函数将继续执行,然后结束返回void。此时,您的
HKSampleQuery
仍在执行。完成后,它将通过完成处理程序发布结果。因此,如果要对结果执行任何操作,需要在完成处理程序中执行。所以你的功能是

func readWeight(completion: @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!)
        completion(0.0)
    }

    guard let results = results else {
        print("No results of query")
        completion(0.0)
    }

    if (results.count == 0) {
        print("Zero samples")
        completion(0.0)
    }

    guard let bodymass = results[0] as? HKQuantitySample else {
        print("Type problem with weight")
        completion(0.0)
    }
    completion(bodymass.quantity.doubleValue(for: HKUnit.pound()))
}

   healthKitStore.execute(weightQuery)
}
要使用结果,请执行以下操作:

self.readWeight(){ (result) in
    //This result is a double value that is returned from HKSampleQuery
}

您试图在传递给
HKSampleQuery
的块中返回某个不起作用的内容,该块没有返回类型。您还必须为方法
readWeight
使用完成块,或者以某种方式等待
HKSampleQuery
的完成。该
HKSampleQuery
函数是否为同步函数?如果是这样,您需要使用block和completionhandler@FangmingNing刚刚了解到它是异步的,所以我必须等待值。。。这似乎有效,但比较起来似乎不起作用发布了我的答案这种方法有效,但我在比较时遇到了这个错误
/Users/xxx/Desktop/xx/xx/ViewController.swift:161:27:二进制运算符“==”不能应用于“(@escaping(Double)->Void)->”()'和'Double'
执行类似于
if(self.readWeight==0.0)的操作时