-second edit- I'm now using the suggestion from Arpit Jain but how do I call that function? I want the result of the function parsed to a variable.
getToken(completionHandler: { // here I should do something with in
// what should I do here to pass the result of getToken to variable?
})
-editted original question-
I'm a complete newby on Swift and trying to implement an API (as a learning project). But since I'm not familiar with the syntax I keep getting errors in this PoC.
I have a http request via alamofire and I expect testResult to either contain "succes" or an erro. But the function keeps returning "empty"
And I have no clue why?
func getToken() -> String{
var testResult: String! = "empty"
let url: String! = "https://the.base.url/token"
let parameters: Parameters = [
"grant_type":"password",
"username":"the_username",
"password":"the_password",
"client_id":"the_clientID",
"client_secret":"the_secret",
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .methodDependent)).validate().responseJSON { response in
switch response.result {
case .success:
testResult = "succes"
case .failure(let error):
testResult = error as! String
}
}
return testResult // this line gives the error
}