ios - Swift: A function amends value of variable, I want to use this variable elsewhere to update UI -


have been looking around answer this. feels basic, must doing wrong.

i have function, using alamofire pulls in json feed, parses it. want use elements update various labels in ui.

func collectdata(){       alamofire.request(.get, "http://xxxxxxxx.co.uk/xxxx.php")         .responsejson { response in             print(response.request)  // original url request              if let jsontest = response.result.value {                  let json = json(jsontest)                   if let name = json[0]["name"].string {                  }               }     }   }  collectdata()  usernamelabel.text = name 

obviously, function runs, , within function can print((name)) or print((jsontest)) , both print expected. how use updated variable within function elsewhere in project.

if put usernamelabel.text = name in function, works fine.

i can create constant outside of method, , outside of class affects initial value, not amended one.

thanks!

edit:

so i've discovered happening because alamofire , swiftyjson asyncronous, , variables in request work. i've tried put code request. of has worked, looks bit of mess.

also, want pull url database , on button click, goto url. don't see how can that, can't have ibaction in request....

global variables solution. create global variable hold entire json response , can parse desired values ever within class want.

global variables variables defined outside of function, method, closure, or type context.global constants , variables computed lazily


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -