0

I need to perform a GET request as attribute (outside viewDidLoad() ) in order to access some filed in other function. This is what I tried:

var my_id: Int!
var note: [String]? {
    didSet{
        let x = service()
        x.getStuff(id: my_id ) { (response, error) in
            note = response?.results
        }
    }
}

I'm a beginner and I don't know how to do this. (I know this is not working) My_id is here with a performSegue from another view. Thank you!

jamesCode
  • 1
  • 10
  • `getStuff` appears to be an asynchronous process, so it takes time before `note` will be set. The approach you have here isn't suitable for asynchronous requests – Paulw11 Mar 22 '20 at 21:59
  • So what's the best approach? What should I do – jamesCode Mar 22 '20 at 22:00
  • You can take a look at the answers in the duplicate, but essentially you need to call `getStuff` and do whatever you need with the data in the closure itself. Your current code doesn't make much sense anyway, because a `didSet` will fire when `note` is set, not when it is read, and because you set `note` in the closure, you would have an infinite recursive loop – Paulw11 Mar 22 '20 at 22:05

0 Answers0