i have run this code but not print any data in console and not get data in my api so no data display in may simulators
func downloadJSON(completed: @escaping () -> ()){
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) {data, response, error in
guard let data = data, error == nil else {return}
//print(data)
if error == nil {
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
print(json)
self.detail = try JSONDecoder().decode([apiData].self, from: data)
DispatchQueue.main.async {
completed()
}
}
}catch{
print("JSON Error")
}
}
}.resume()
}
}
func downloadJSON(completed: @escaping () -> ()){
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) {data, response, error in
guard let data = data, error == nil else {return}
//print(data)
if error == nil {
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
print(json)
self.detail = try JSONDecoder().decode([apiData].self, from: data)
DispatchQueue.main.async {
completed()
}
}
}catch{
print("JSON Error")
}
}
}.resume()
}
}
if let json = try JSONSerialization ... as? [String: Any]
contradictsJSONDecoder().decode([apiData].self
. Ie, you are expecting to decode withJSONDecoder
an Array, but with JSONSerialization a Dictionary. Remove theif let json
line, or at least add anelse { print("Response is not a JSON OR a [String: Any]"); print("Received content: \(String(data: data, encoding: .utf8)")}
Dictionary
at top level, soprint(json)
should be called. But iserror
notnil
? Isdata
not nil?