I called an API for rate currency, my variable "taux" is define at the begin of the class. So when I define the value of "taux" the value is great ( first print(taux)) but at the end ( last print(taux) ) the value of the variable is not the same, it's the old value of the variable. Just for information I checked the problem does not come from the for loop.
if let url = URL(string: "https://api.exchangeratesapi.io/latest?base=CAD") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let res = try JSONDecoder().decode(Currency.self, from: data)
for rate in res.rates{
if rate.key == self.codeDevise{
self.taux = rate.value
print(self.taux)
}
}
} catch let error {
print(error)
}
}
}.resume()
}
print(self.taux)