Answer the question
In order to leave comments, you need to log in
Why doesn't Xcode see the variable?
Good day, there is such a problem, I make a request to the server, I get a response in the form of JSON, I parse JSON and save it all to a variable
Alamofire.request("http://127.0.0.1:8000/api/auditOS/", method: .get).responseJSON { response in
let jsonParse = JSON(response.result.value!)
print(jsonParse)
}
Answer the question
In order to leave comments, you need to log in
Judging by what you wrote, the answer is extremely simple: you declare a jsonParse constant inside a closure. You can see it inside the closure. This problem can be solved by declaring a variable (var instead of let) outside of functions.
Because here is an asynchronous model and this variable can only be used in callback, where you have print.
Roughly speaking, this callback will (most likely) be executed in another thread, in parallel with the code that after it was created, so if that code accessed this variable, it would catch nil.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question