title: decoding a json to generic array or class in swift
body: <p>How do you decode json to a generic model in swift?<br>
  In java for decoding json I use GSON and in general it does not matter I use <code>&lt;T&lt;E&gt;&gt; or ArrayList&lt;E&gt;</code>.In swift Array is a struct and can't be inheritance and it has not implemented Decodable.</p>
  
  <p>I'm looking for a generic elegant class to use in all my web service. </p>
  
  <p>My scenario:<br>
  I have json response</p>
  
  <pre><code>{
  "status": true,
  "message": "",
  "code": 200,
  "response": [{
      "id": 43
  }]
  }
  </code></pre>
  
  <p>and a generic reponse model like this from web services:</p>
  
  <pre><code>class GeneralResponse&lt; T : Decodable &gt;:NSObject,Decodable{
  
      var status = false
      var message = ""
      var code = -1
      var response : T?
  
      private enum CodingKeys: String, CodingKey {
          case status
          case message
          case code
          case response
      }
  
      required public init(from decoder: Decoder) throws{
          let container = try decoder.container(keyedBy: CodingKeys.self)
          status = try container.decode(Bool.self, forKey: .status)
          message = try container.decode(String.self, forKey: .message)
          code = try container.decode(Int.self, forKey: .code)
          response = try container.decode(T.self, forKey: .response)
      }
  
  }
  class ItemDemoModel:Decodable {
       var id = -1
      private enum ItemDemModelCodingKeys : String, CodingKey {
          case id
      }
       required init(from decoder:Decoder) throws {
          let container = try decoder.container(keyedBy: ItemDemModelCodingKeys.self)
          id = try container.decode(Int.self, forKey: .id)
      }
  }
  </code></pre>
  
  <p>response variable can be ItemDemoModel or an array of ItemDemoModel.<br>
  For example:<br>
  It can be <code>GeneralResponse&lt;Array&lt;ItemDemoModel&gt;&gt;&gt;</code> 
  or <code>GeneralResponse&lt;ItemDemoModel&gt;&gt;</code></p>
  
  <p>thanks.</p>