swift - String nil inside NSURLSession -
i declare string in beginning
var teststring: string? let task = nsurlsession.sharedsession().datataskwithurl(url!) {(data, response, error) in let xml = swxmlhash.parse(data) teststring = xml["root"]["schedule"]["date"].element?.text }
but outside nsurlsession, teststring
nil. how can make not become nil , can use value?
for example, want use
println (teststring)
after method block. nil
the reason variable nil because closures executed asynchronously. means rest of code after network request continue called normal, code containing parameters data, response , error called when network request finished.
to work around this, try putting whatever trying variable inside closure, println(teststring)
inside curly brackets.
Comments
Post a Comment