title: Swift. How to write bytes from NSData into another NSData?
body: <p>I'm trying to concatenate two <code>NSData</code> objects into one <code>NSMutableData</code>, and than get them back. For now i'm trying to do it in such way:</p>
  
  <ol>
  <li>Get length of first object.</li>
  <li>Write into <code>NSMutableData</code> in such order: first object length, first object, second object.</li>
  </ol>
  
  <p>Code looks like:</p>
  
  <pre><code>let firstString = "first_string";
  let secondString = "secondSting";
  
  let firstData = firstString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  let secondData = secondString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  
  
  let mutableData = NSMutableData()
  
  var length = firstData.length
  
  mutableData.appendBytes(&amp;length, length: sizeof(Int))
  
  mutableData.appendData(firstData)
  
  mutableData.appendData(secondData)
  </code></pre>
  
  <p>Then I want to get datas back. So I suppose to read first data length and then get two datas.</p>
  
  <pre><code>    var length = 0
      mutableData.getBytes(&amp;length, length: sizeof(Int))
  </code></pre>
  
  <p>But when I'm trying to get data I'm getting crash instead:</p>
  
  <pre><code>    var data = NSData()
  
      mutableData.getBytes(&amp;data, range: NSMakeRange(sizeof(Int), length))
  </code></pre>
  
  <p>Maybe somebody know where is my problem or how to get datas?</p>