title: Deserialize JSON array based on nested type attribute
body: <p>Consider this example JSON:</p>
  
  <pre><code>{
      "sections": [{
          "title": "Sign up",
          "rows": [
              {
                  "type": "image",
                  "imageURL": "https://example.com/image.jpg"
              },
              {
                  "type": "textField",
                  "value": "",
                  "placeholder": "Username"
              },
              {
                  "type": "textField",
                  "placeholder": "password"
              },
              {
                  "type": "textField",
                  "placeholder": "confirmPassword"
              },
              {
                  "type": "button",
                  "placeholder": "Register!"
              }
          ]
      }]
  }
  </code></pre>
  
  <p>Let's say I wanted to parse the JSON above into the following models <em>(I know it doesn't compile due to the <code>Row</code> protocol not corresponding to <code>Decodable</code>)</em>:</p>
  
  <pre><code>enum RowType: String, Codable {
      case textField
      case image
      case button
  }
  
  protocol Row: Codable {
      var type: RowType { get }
  }
  
  struct TextFieldRow: Row {
      let type: RowType
      let placeholder: String
      let value: String
  
      enum CodingKey: String {
          case type
          case placeholder
          case value
      }
  }
  
  struct ImageRow: Row {
      let type: RowType
      let imageURL: URL
  
      enum CodingKey: String {
          case type
          case imageURL
      }
  }
  
  struct ButtonRow: Row {
      let type: RowType
      let title: String
  
      enum CodingKey: String {
          case type
          case title
      }
  }
  
  struct Section: Codable {
      let rows: [Row]
      let title: String
  
      enum CodingKey: String {
          case rows
          case title
      }
  }
  
  struct Response: Codable {
      let sections: [Section]
  
      enum CodingKey: String {
          case sections
      }
  }
  
  
  // Parsing the response using the Foundation JSONDecoder
  let data: Data // From network
  let decoder = JSONDecoder()
  do {
      let response = try decoder.decode(Response.self, from: data)
  } catch {
      print("error: \(error)")
  }
  </code></pre>
  
  <p>Is there a way to make the Swift code above Codable compliant? 
  I know you can manually solve this by first grabbing each <code>Row</code>'s <code>type</code> string and then creating the right type of <code>Row</code> model as well as changing them from structs to classes and letting the <code>Row</code> protocol be a superclass instead. But is there a way that requires less manual labour?</p>