SwiftUI beginner here. I am trying to set the value of a variable by calling a method inside a function. Although the method is evaluating correctly, I am not able to return the value of the variable. Please see my code:
let headers: HTTPHeaders = [
"refreshtoken": refreshToken
]
let sessionManager = Alamofire.Session.default
let request = sessionManager.request(url, method: .post, headers: headers)
var ttrr: String = "default"
request.responseJSON { (response) in
switch response.result {
case .success( _):
let json = JSON(response.value!) //SwiftyJSON
let tr = json[0]["TRCRate"].double!
ttrr = String(tr)
print("ttrr in request: \(ttrr)")
case .failure(let error):
print("API Manager Error: \(error)")
}
}
//5. Return the ready-to-use data
print("ttrr before return: \(ttrr)")
return ttrr
}
And here is the print output:
ttrr bfore return: default
ttrr in request: 0.18
Could someone kindly point to what I am doing wrong? Thanks in advance.