title: Copy contents of Swift Array to Struct embedded Tuple
body: <p>For communicating with a BLE characteristic, I have a Swift struct that looks like:</p>
  
  <pre><code>struct Packet {
      var control1:UInt8 = 0
      var control2:UInt8 = 0
      var payload:(UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8,UInt8) = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
  
      init(control1:UInt8, control2:UInt8) {
          self.control1 = control1
          self.control2 = control2
      }
  }
  </code></pre>
  
  <p>I have <code>payload</code> defined as a tuple, because that seems to be the only way to have an array (of bytes in this case) of fixed size embedded in a Swift struct. Verbose, but whatever.</p>
  
  <p>I have a big ol' <code>source:[UInt8]</code> that I want to pull swatches of into that <code>Packet</code> struct, so I can send them via BLE to the remote device. When I do:</p>
  
  <pre><code>var packet = Packet(control1: self.pageIndex, control2: sentenceIndex)
  let offset = (Int(self.pageIndex) * self.pageSize) + (Int(sentenceIndex) * self.sentenceSize)
  let limit = offset + self.sentenceSize
  packet.payload = self.source[offset..&lt;limit]
  </code></pre>
  
  <p>For the last line, I get the rather confusing error:</p>
  
  <pre><code>Cannot subscript a value of type '[UInt8]'
  </code></pre>
  
  <p>Cryptic I say, because it actually can. If I take the assignment to the <code>packet.payload</code> out, it has no problem subscripting the value.</p>
  
  <p>What I'm really interested in at a higher level, is how one puts together a struct with a fixed size array of bytes, and then copies swatches of a large buffer into those. I would like to both understand the above, as well as know how to solve my problem.</p>
  
  <p>UPDATE:</p>
  
  <p>I ended up backing up a little, influenced by both answers below, and rethinking. My main driving force was that I wanted a simple/clever way to have convert a struct with an internal array to/from NSData, primary in BLE communications. What I ended up doing was:</p>
  
  <pre><code>struct Packet {
      var pageIndex:UInt8 = 0
      var sentenceIndex:UInt8 = 0
      var payload:ArraySlice&lt;UInt8&gt; = []
  
      var nsdata:NSData {
          let bytes:[UInt8] = [self.pageIndex, self.sentenceIndex] + self.payload
          return NSData(bytes: bytes, length: bytes.count)
      }
  }
  </code></pre>
  
  <p>Not the most efficient because I have to create the intermediate [UInt8] array, but I decided that a simple way to convert didn't exist, that I'd have to do things with as conversions or memcpy and friends.</p>
  
  <p>I'm not sure which of the two below to mark as an answer, since both influenced what I ended up with.</p>