My code ran in the wrong order. The aerror
was printed at the beginning, it should be printed at the end.
Here is my code
var aerror :Int?
NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: url), queue: NSOperationQueue.mainQueue(), completionHandler: { (res, data, error) -> Void in
let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("\n********\nData\n*******")
print(str)
print("\n********\nData\n*******")
let json = JSON(data: data!)
aerror = json["Logon"]["error"].int!
print("\n********\njson\n*******")
print(json)
print("\n********\njson\n*******")
})
print("\n********\naeeror=\(aerror)")
Here is output
aeeror=nil
********
Data
*******
Optional({"func":"LogonJs","Logon":{"error":2}})
********
Data
*******
********
json
*******
{
"Logon" : {
"error" : 2
},
"func" : "LogonJs"
}
********
json
*******
I don't know why the aerror
was printed first.
I Want print the aerror
before I return it.
Can anyone help me fix it?