title: Swift structs to NSData and back
body: <p>I have a struct containing a struct and an <code>NSObject</code> that I want to serialize into an <code>NSData</code> object:</p>
  
  <pre><code>struct Packet {
    var name: String
    var index: Int
    var numberOfPackets: Int
    var data: NSData
  }
  
  var thePacket = Packet(name: name, index: i, numberOfPackets: numberOfPackets, data: packetData)
  </code></pre>
  
  <p>How do I best serialize the Packet into an <code>NSData</code>, and how do I best deserialize it?</p>
  
  <p>Using</p>
  
  <pre><code>var bufferData = NSData(bytes: &amp; thePacket, length: sizeof(Packet))
  </code></pre>
  
  <p>of only gives me the pointers of name and data. I was exploring <code>NSKeyedArchiver</code>, but then I'd have to make Packet an object, and I'd prefer to keep it a struct.</p>
  
  <p>Cheers</p>
  
  <p>Nik</p>