1

In a variable which is used for a table view data source array controller, I'm trying to get a value with a asynchronous function:

 dynamic var price: Double {
        get {
            if id == "EUR" {
                PriceQuery.shared.loadprice("JPY") { price in
                    return price
                }  
            }
        return 0.0
        }
    }

I'm getting the error: "unexpected non-void return value in a void function". Although it makes sense that the downer return 0.0 doesn´t "know" it has to "wait" for the upper asynchronous return, I don´t know how I could solve the problem.

There is already an answer about how to return asynchronously in a function. But I can't use a completion handler inside a variable, right?

jscs
  • 63,694
  • 13
  • 151
  • 195
Josch Hazard
  • 323
  • 3
  • 20
  • 1
    You can’t. So you’ll have to change how you implement this. But you’re still trying to return a value inside an asynchronous function which can’t be done. The exact scenario is different but the answer is the same. You can’t return a value from a function that uses an async call to get the value. – Fogmeister Dec 02 '17 at 15:28
  • I suggest changing your var into a func with a completion handler. – Fogmeister Dec 02 '17 at 15:29
  • @Fogmeister Could you give me advice how to implement this? – Josch Hazard Dec 02 '17 at 15:29
  • @Fogmeister But the Arraycontroller cannot access a function. It needs a variable. – Josch Hazard Dec 02 '17 at 15:30
  • 1
    Then it can’t be asynchronous. You can’t access an asynchronous value synchronously. – Fogmeister Dec 02 '17 at 15:31
  • @Fogmeister So this question is not a dublicate. Maybe someone else has an idea for a workaraound. – Josch Hazard Dec 02 '17 at 15:33
  • The question you asked is a duplicate. Perhaps what you want to ask is how to get that value and put it into your array controller. ... which is an entirely different question. – Fogmeister Dec 02 '17 at 15:34
  • 1
    See the other question that I just added to the duplicate list. – jscs Dec 02 '17 at 15:35

0 Answers0