title: Codable object mapping array element to string
body: <p>I have a (annoying) situation where my back-end returns an object like this:</p>
  
  <pre><code>{
  "user": {
          "name": [
              "John"
          ],
          "familyName": [
              "Johnson"
          ]
      }
  }
  </code></pre>
  
  <p>where each property is an array that holds a string as its first element. In my data model <code>struct</code> I could declare each property as an array but that really would be ugly. I would like to have my model as such:</p>
  
  <pre><code>struct User: Codable {
      var user: String
      var familyName: String
  }
  </code></pre>
  
  <p>But this of course would fail the encoding/decoding as the types don't match. Until now I've used <code>ObjectMapper</code> library which provided a <code>Map</code> object and <code>currentValue</code> property, with that I could declare my properties as <code>String</code> type and in my model <code>init</code> method assig each value through this function:</p>
  
  <pre><code>extension Map {
      public func firstFromArray&lt;T&gt;(key: String) -&gt; T? {
          if let array = self[key].currentValue as? [T] {
              return array.first
          }
          return self[key].currentValue as? T
      }
  }
  </code></pre>
  
  <p>But now that I am converting to <code>Codable</code> approach, I don't know how to do such mapping. Any ideas?</p>