努力弄清楚如何处理具有不同类型和结构的可解码数组。本质上它是一个数据数组,这导致了一个包含 4 种类型的数组,并且这些类型中的每一种都包含进一步的搜索结果数组。
您可以将 decodeable 用于具有不同格式的数组吗?还是只是使用对象字典?
我将在底部附上我的 JSON
struct SearchData: Decodable {
var success: Bool
var server_response_time: Int
var data: [SearchDataType]
}
//This is the array of 3 of the group types: "Movies", "TV-Shows", "Artists"
struct SearchDataType: Decodable {
let group: String
let data: [SearchDataMovies]
}
// Where group = "Movies"
struct SearchDataMovies: Decodable {
let title: String
let year: String
}
// Where group = "TV-Shows"
struct SearchDataTV: Decodable {
let title: String
let year: Int
}
// Where group = "Artists"
struct SearchDataArtists: Decodable {
let name: String
}