SHARE
TWEET

Untitled

a guest Nov 14th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct NatureThings: Decodable {
  2.     let things: [NatureThing]
  3.  
  4.     init(from decoder: Decoder) throws {
  5.         var container = try decoder.unkeyedContainer()
  6.         var things: [NatureThing] = []
  7.         while !container.isAtEnd {
  8.             let superDecoder = try container.superDecoder()
  9.             let typeContainer = try superDecoder.container(keyedBy: NatureThingTypeKey.self)
  10.             guard let type = NatureThingType(rawValue: try typeContainer.decode(String.self, forKey: .type)) else {
  11.                 continue
  12.             }
  13.  
  14.             switch type {
  15.                 case .mountain:
  16.                     let mountain = try Mountain(from: superDecoder)
  17.                     things.append(mountain)
  18.                 case .lake:
  19.                     let lake = try Lake(from: superDecoder)
  20.                     things.append(lake)
  21.                 case .cabin:
  22.                     let cabin = try Cabin(from: superDecoder)
  23.                     things.append(cabin)
  24.             }
  25.         }
  26.         self.things = things
  27.     }
  28. }
RAW Paste Data
Top