即使将Swift设置为Double,Swift也会给出非void返回错误?

时间:2017-06-22 13:46:30

标签: swift function swift3 return double

我收到此错误:

/Users/xxxx/Desktop/xxx/xxx/ViewController.swift:43:38: Unexpected non-void return value in void function

其他人喜欢这样,即使我的函数设置为返回Double。

这是每次返回时出现此错误的函数之一。

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())

谢谢!

4 个答案:

答案 0 :(得分:1)

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

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
}

答案 1 :(得分:0)

必须在函数结束时返回Double值。

 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
}

答案 2 :(得分:0)

我想你想要实现的是:

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方法:

readWeight(result: { myResult in
    let comparisonResult = myResult == 0.0
})

这样myResult就是你要找的值。

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

可能你应该阅读更多关于闭包的内容:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

Swift: How to pass in a closure as a function argument

答案 3 :(得分:0)

结尾处的大括号内的行:

let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) { ... }

实际上是作为HKSampleQuery初始值设定项的参数发送的另一个闭包的一部分。将它们视为一个单独的函数,当您调用healthKitStore.execute(weightQuery)时将执行该函数。如Apple's documentation中所述,该闭包的签名是:(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)
}

然后只需致电printWeight()即可获得结果。