Ой! Спасибо, что поймали это.
кто-нибудь сказать мне, что я делаю не так? Я посмотрел на все вопросы здесь, как здесьКак декодировать вложенную структуру JSON по протоколу Swift Decodable? и я нашел тот, который кажется именно то, что мне нужноSwift 4 Кодируемое декодирование JSON.
{
"success": true,
"message": "got the locations!",
"data": {
"LocationList": [
{
"LocID": 1,
"LocName": "Downtown"
},
{
"LocID": 2,
"LocName": "Uptown"
},
{
"LocID": 3,
"LocName": "Midtown"
}
]
}
}
struct Location: Codable {
var data: [LocationList]
}
struct LocationList: Codable {
var LocID: Int!
var LocName: String!
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "/getlocationlist")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error!)
return
}
guard let data = data else {
print("Data is empty")
return
}
do {
let locList = try JSONDecoder().decode(Location.self, from: data)
print(locList)
} catch let error {
print(error)
}
}
task.resume()
}
Я получаю ошибку:
typeMismatch (Swift.Array, Swift.DecodingError.Context (codingPath: [], debugDescription: «Ожидается декодировать массив, но вместо него найден словарь.», underError: nil))