Hello, i'am having a problem which i can't solve....I am trying to get a json code by an API request with URLSessionDataTask with a completion handler. However, my code inside my completion handler is never executed. When i am in debugger mode and execute my App line by line, i can see that all the code inside the completion handler is skipped. Here is my code :
import Foundation
class Market
{
var market: [String] = []
func getMarket()
{
let config = URLSessionConfiguration.default /
let session = URLSession(configuration: config) /
let url = URL(string: "https:/
let task = session.dataTask(with: url, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
do {
print("debut du script json")
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
if let jsondata = json as? NSDictionary,
let results = jsondata["result"] as? NSArray
{
var marketCurrency : NSDictionary!
var currency : String!
var baseCurrency : String!
for i in 0...results.count-1
{
marketCurrency = results[i] as? NSDictionary
currency = marketCurrency["MarketCurrency"] as? String
baseCurrency = marketCurrency["BaseCurrency"] as? String
let coin : String = currency + "-" + baseCurrency
self.market.append(coin)
}
}
} catch {
print("error in JSONSerialization")
}
}
})
task.resume()
}
func returnMarket() -> [String]
{
return self.market
}
}