title: What is Swift enum byte representation?
body: <p>I have the following enum:</p>
  
  <pre><code>enum Enum: UInt8 
  {
    case A = 0x00
    case B = 0x01
    case C = 0x10
  }
  </code></pre>
  
  <p>I use the following code to convert it to <code>NSData</code>:</p>
  
  <pre><code>var value = Enum.C
  let data = NSData(bytes: &amp;value, length: 1)
  </code></pre>
  
  <p>Naturally, I expect <code>data</code> to contain <strong><code>0x10</code></strong>, however, it contains <strong><code>0x02</code></strong>.</p>
  
  <p>For <code>Enum.A</code> it contains <code>0x00</code> and for <code>Enum.B</code> it contains <code>0x01</code>.</p>
  
  <p>To me, it looks like it stores an index of the value instead of its actual raw data. Could someone explain this behavior?</p>
  
  <p>P.S. If I use <code>rawValue</code> it works perfectly. However, I want to understand the reason behind, since because of it I cannot create a generic function to convert values to NSData.</p>