我正在尝试从URL获取JSON,但由于发生错误而无法执行此操作,因此响应和数据为零。
我从网址
获取数据的代码 loadData(urlString: String, completion : @escaping (Player,Player) -> Void) -> Void {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: urlString)
let task = session.dataTask(with: url!){data,response,error in
if error != nil{
print("problem in loading data");
print(error.debugDescription);
} else{
let players = self.parseJson(data: data as NSData?)
let priority = DispatchQueue.global(qos: .userInitiated)
priority.async {
DispatchQueue.main.async {
completion(players.0, players.1);
}
}
}
}
task.resume();