title: Swift: accessing struct variables with a String
body: <p>I am looking for a better way to access the variables of a struct. A json query fills an array of this struct:</p>
  <pre><code>    struct AnimalInfo: Codable {
          var English: String
          var French: String
          var Spanish: String
      }
  </code></pre>
  <p>An example:</p>
  <pre><code>    let theDog = AnimalInfo(English: &quot;Dog&quot;, French: &quot;Chien&quot;, Spanish: &quot;Perro&quot;)
  </code></pre>
  <p>The languages are in an array, the current one is always changing. I keep track with a pointer:</p>
  <pre><code>    let languageListArray = [&quot;English&quot;, &quot;French&quot;, &quot;Spanish&quot;]
      let currentLanguage = languageListArray[2]
  </code></pre>
  <p>I'd like to be able to retrieve the value from the struct with this information, something along the lines of:</p>
  <pre><code>    let myString = theDog.currentLanguage // = &quot;Perro&quot;
  </code></pre>
  <p>.. but of course that is not possible. The best I've come up with so far is a switch:</p>
  <pre><code>    switch currentLanguage {
          case &quot;English&quot;:
              return theDog.English
          case &quot;French&quot;:
              return theDog.French
          case &quot;Spanish&quot;:
              return theDog.Spanish
      }
  </code></pre>
  <p>But this is ugly, and doesn't scale well with many languages! Anyone have a better way?</p>