I don't recommend that you structure your JSOn with heterogenous types; in theis case details.name can be either a string or an array of Int. While you can do this is Swift, its kind of messy since its a statically typed language by default. In the event you can't change your JSON to something cleaner here a playground showing is how you opt into dynamic behavior with Any.
//: Playground - noun: a place where people can play
import PlaygroundSupport
import UIKit
let json = """
{
"name": "John Doe",
"details": [{
"name": "exampleString"
}, {
"name": [1, 2, 3]
}]
}
"""
struct Detail {
var name: Any?
var nameString: String? {
return name as? String
}
var nameIntArray: [Int]? {
return name as? [Int]
}
enum CodingKeys: CodingKey {
case name
}
}
extension Detail: Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if let string = name as? String {
try container.encode(string, forKey: .name)
}
if let array = name as? [Int] {
try container.encode(array, forKey: .name)
}
}
}
extension Detail: Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if let string = try? values.decode(String.self, forKey: .name) {
name = string
} else if let array = try? values.decode(Array<Int>.self, forKey: .name) {
name = array
}
}
}
struct Record: Codable {
var name: String
var details: [Detail]
}
let jsonDecoder = JSONDecoder()
let record = try! jsonDecoder.decode(Record.self, from: json.data(using: .utf8)!)
print("\(record.details.first!.name!) is of type: \(type(of:record.details.first!.name!))")
print("\(record.details.last!.name!) is of type: \(type(of:record.details.last!.name!))")
the output is:
exampleString is of type: String
[1, 2, 3] is of type: Array<Int>
init(from: Decoder)and implement your own logic there.