title: How can I access a struct member by passing its name as a function parameter in Swift?
body: <p>(I know the question has been asked here: <a href="https://stackoverflow.com/questions/34994529/access-a-struct-property-by-its-name-as-a-string-in-swift">Access a struct property by its name as a string in Swift</a> but I'd rather not use a switch/case statement and the struct is mandatory.)</p>
  
  <p>I have a function:</p>
  
  <pre><code>func computeStat(_ pokemon: Pokemon,_ stat: String) {
    let level = 1
    return Int((Int(((2*pokemon.species.base_values.stat + pokemon.stat+Int(pokemon.effort_values.stat/4))*pokemon.level)/100)+5)*level)
  }
  </code></pre>
  
  <p>As you can see I'd like to call the member "stat" but for different structs:</p>
  
  <ul>
  <li>base_values</li>
  <li>pokemon</li>
  <li>effort_values</li>
  </ul>
  
  <p>Stat can take 5 different values (attack, defense, etc). I'm calling the function in a getter:</p>
  
  <pre><code>  get {
      let stats = Stats(
          hitpoints: computeHp(self),
          attack: computeStat(self, "attack"),
          defense: computeStat(self, "defense"),
          special_attack: computeStat(self, "special_attack"),
          special_defense: computeStat(self, "special_defense"),
          speed: computeStat(self, "speed")
      )
  
      return stats
    }
  </code></pre>
  
  <p>Of course my function, as is, doesn't work because I'm trying to access the member of a struct with a String.</p>
  
  <p>Does anyone have a way to do it?</p>