0

first of all, I'm sorry if the question is not clear. I don't exactly know how to describe my problem here. I fetch JSON files from a website API, and i process the files with JSONDecoder. I can print the result when inside do catch but I cant assign to either global or local variable, the debugger state variable was capture in closure before initialize

i'll edit the question once i know how to properly name the question.

func indodaxExtraction() -> apiIndodax{
    let urlLink = "https://indodax.com/api/btc_idr/ticker"
    let url = URL(string: urlLink)
    URLSession.shared.dataTask(with: url!) { (data, response, err) in
        guard let data = data else {return}
        do {
            let api = try JSONDecoder().decode(apiIndodax.self, from: data)
            print(api.ticker.high)
            print(api.ticker.low)
            print(api.ticker.last)
        }catch let jsonERR{
            print(jsonERR)
        }
}.resume()

}

I'm new in swift, just learn it for about 1 weeks. Sorry for any mistake.

Fernando Ciam
  • 21
  • 2
  • 6
  • What you are trying to do is impossible. You cannot return an `apiIndodax` from the function that fetches that `apiIndodax` using `URLSession.shared.dataTask`, because the function returns _before_ the fetch. – matt Aug 01 '18 at 02:35
  • Okay, so what should i do to fetch the data first then put into struct and return it – Fernando Ciam Aug 01 '18 at 02:40
  • @FernandoCiam Review the duplicates at the top of your question. The answers to those show you the proper solution. – rmaddy Aug 01 '18 at 02:49

0 Answers0