title: How to use generic value in CodingKeys enum (Codable Protocol)
body: <p>Compiler complains: &quot;Raw value for enum case must be a literal&quot;</p>
  <p>In the struct/class we can use generic value but how to use generic in <code>enum</code>.</p>
  <p>The below is the server response that I want to make a general structure</p>
  <pre><code>{
    &quot;status&quot;: true,
    &quot;message&quot;: &quot;message&quot;,
    &quot;error&quot; : &quot;error if any&quot;
    &quot;any key&quot; : &quot;Any kind of data&quot;
  }
  </code></pre>
  <p>In above example the &quot;any key&quot; part is tricky one. &quot;any key&quot; will be different for different service call.</p>
  <p><strong>for user city list:</strong></p>
  <pre><code>{
    &quot;status&quot;: true,
    &quot;message&quot;: &quot;message&quot;,
    &quot;error&quot; : &quot;&quot;
    &quot;cities&quot; : &quot;city list&quot;
  }
  </code></pre>
  <p><strong>for user state list:</strong></p>
  <pre><code>{
    &quot;status&quot;: true,
    &quot;message&quot;: &quot;message&quot;,
    &quot;error&quot; : &quot;&quot;
    &quot;states&quot; : &quot;state list&quot;
  }
  </code></pre>
  <p><strong>for user posts:</strong></p>
  <pre><code>{
    &quot;status&quot;: true,
    &quot;message&quot;: &quot;message&quot;,
    &quot;error&quot; : &quot;&quot;
    &quot;posts&quot; : &quot;list of posts&quot;
  }
  </code></pre>
  <p>An you can see every service call has same key for &quot;status&quot;, &quot;message&quot; and &quot;error&quot; and <em><strong>the data has different keys &quot;cities&quot;, &quot;states&quot;, &quot;posts&quot;, etc.</strong></em></p>
  <p>So I want to create a general struct to include all these in to one.</p>
  <p>I did the following way but stuck at different keys.</p>
  <pre><code>struct Response&lt;T&gt;: Codable  {
  
  
  let message : String? // common in every service call
  let status : Bool? // common in every service call
  let errors: String? // common in every service call
  let data : T? // it will be different for every call
  
  enum CodingKeys: String, CodingKey {
      
      case message = &quot;message&quot;
      case status = &quot;status&quot;
      case data = &lt;key&gt;    //Here what to use?
      case errors = &quot;errors&quot;
  }
  
  init(from decoder: Decoder) throws {
      let values = try decoder.container(keyedBy: CodingKeys.self)
      
      message = try values.decodeIfPresent(String.self, forKey: .message)
      status = try values.decodeIfPresent(Bool.self, forKey: .status)
      errors = try values.decodeIfPresent(String.self, forKey: .errors)
      data = try T(from: decoder)
  }
  
  }
  </code></pre>
  <p>Is it possible what I'm trying to do?</p>
  <p>If yes, how to achieve this?</p>
  <p>Any help will be appreciated!!</p>
  <p>I got following errors when I was trying..</p>
  <blockquote>
  <ol>
  <li><p>Raw value for enum case must be a literal</p>
  </li>
  <li><p>Non-nominal type 'T' does not support explicit initialization</p>
  </li>
  </ol>
  </blockquote>