Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct NatureThings: Decodable {
- let things: [NatureThing]
- init(from decoder: Decoder) throws {
- var container = try decoder.unkeyedContainer()
- var things: [NatureThing] = []
- while !container.isAtEnd {
- let superDecoder = try container.superDecoder()
- let typeContainer = try superDecoder.container(keyedBy: NatureThingTypeKey.self)
- guard let type = NatureThingType(rawValue: try typeContainer.decode(String.self, forKey: .type)) else {
- continue
- }
- switch type {
- case .mountain:
- let mountain = try Mountain(from: superDecoder)
- things.append(mountain)
- case .lake:
- let lake = try Lake(from: superDecoder)
- things.append(lake)
- case .cabin:
- let cabin = try Cabin(from: superDecoder)
- things.append(cabin)
- }
- }
- self.things = things
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement