2017-06-22 73 views
-1

我收到此錯誤:即使Swift被設置爲Double,Swift也會給出無效的返回錯誤?

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

謝謝!

+0

你試圖在你傳遞給'HKSampleQuery'塊返回的東西,那是行不通的,塊呢沒有返回類型。你必須爲你的readWeight方法使用完成塊,或者以某種方式等待完成'HKSampleQuery'。 – luk2302

+0

是'HKSampleQuery'函數是同步的嗎?如果是這樣,你需要使用塊和完成處理程序 –

+0

@FangmingNing剛學會它是異步的,所以我不得不等待值...這個https://stackoverflow.com/a/44701636/4285493似乎工作,但比較它似乎並沒有工作 – WebMaster

回答

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 
} 
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

+0

這種工作,但我得到這個錯誤時,它比較它'/Users/xxx/Desktop/xx/xx/ViewController.swift:161:27:二進制運算符'=='不能應用於'if(self.readWeight == 0.0)' – WebMaster

+0

@WebMaster類型'(@escaping(Double) - > Void) - >()'和'Double''的操作數請檢查更新回答 – user3581248

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()得到的結果。

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 
} 
相關問題