Possibly this could be a misunderstanding that I have in Swift. I'm new to the language and my background is in Perl, which makes me feel like swift is acting differently.
I have 2 files. ViewController.swift and APICalls.swift. In viewcontroller I have a function for a button. In this function I'm making a call to another function in the APICalls. When I do, my println() in the APICalls is printing the correct data, however, it's doing so after my println() in the button function.
Viewcontroller.swift
@IBAction func buttonStuff(sender: AnyObject) {
var api = APICalls()
var token:String
token = api.TEST("letmein")
println("\ntokenDidLOAD = \(token)\n")
}
APICalls.swift
class APICalls {
func TEST(command: String) -> (String) {
var token:String = ""
// Form URL-Encoded Body
let bodyParameters = [
"COMMAND":command,
]
let encoding = Alamofire.ParameterEncoding.URL
// Fetch Request
Alamofire.request(.POST, "http://api.com/?v=1", parameters: bodyParameters, encoding: encoding)
.validate(statusCode: 200..<300)
.responseJSON{(request, response, data, error) in
if (error == nil)
{
let json = JSON(data!)
token = json["TOKEN"].string!
println("jsonAPI = \(json) \n\n")
}
else
{
println("HTTP HTTP Request failed: \(error)")
}
}
return token
}
}
Here is my output
tokenDidLOAD =
jsonAPI = {
"STATUS" : "OK",
"TOKEN" : "698798765432134654",
}
I don't understand why 'tokenDidLOAD' is printing first before the jsonAPI.