Avatar
omochimetaru 3/12/2021 5:07 AM
@swift-5.3.3 import Foundation struct A: Encodable { var a1: Int = 1 var a2: Int = 2 } struct B: Encodable { var b1: Int = 3 var b2: Int = 4 } struct AAndB: Encodable { var a: A = .init() var b: B = .init() func encode(to encoder: Encoder) throws { try a.encode(to: encoder) try b.encode(to: encoder) } // func encode(to encoder: Encoder) throws { // do { // var c = encoder.singleValueContainer() // try c.encode(a) // } // do { // var c = encoder.singleValueContainer() // try c.encode(b) // } // } } func main() throws { let x = AAndB() let e = JSONEncoder() let j = try e.encode(x) print(String(decoding: j, as: UTF8.self)) } try! main()