title: Objective C to Swift code conversion, can't seem to treat bytes correctly in Swift
body: <p>I'm working on a simple Swift bluetooth heart rate monitor iOS App. I found <a href="http://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor" rel="nofollow">this</a> great tutorial which has objective C code. I've converted it to Swift and I'm getting data from my heart rate monitor. My problem is that I can't seem to correctly access and convert the byte data in Swift.</p>
  
  <p>Here's the Objective C code:</p>
  
  <pre><code>// Instance method to get the heart rate BPM information
  - (void) getHeartBPMData:(CBCharacteristic *)characteristic error:(NSError *)error
  {
      // Get the Heart Rate Monitor BPM
      NSData *data = [characteristic value];      // 1
      const uint8_t *reportData = [data bytes];
      uint16_t bpm = 0;
  
      if ((reportData[0] &amp; 0x01) == 0) {          // 2
          // Retrieve the BPM value for the Heart Rate Monitor
          bpm = reportData[1];
      }
      else {
          bpm = CFSwapInt16LittleToHost(*(uint16_t *)(&amp;reportData[1]));  // 3
      }
      // Display the heart rate value to the UI if no error occurred
      if( (characteristic.value)  || !error ) {   // 4
          self.heartRate = bpm;
          self.heartRateBPM.text = [NSString stringWithFormat:@"%i bpm", bpm];
          self.heartRateBPM.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:28];
          [self doHeartBeat];
          self.pulseTimer = [NSTimer scheduledTimerWithTimeInterval:(60. / self.heartRate) target:self selector:@selector(doHeartBeat) userInfo:nil repeats:NO];
      }
      return;
  }
  </code></pre>
  
  <p>Here's the Swift Code:</p>
  
  <pre><code>func peripheral(peripheral: CBPeripheral!,
      didUpdateValueForCharacteristic characteristic: CBCharacteristic!,
      error: NSError!) -&gt; String
  {
  
      // Get the Heart Rate Monitor BPM
      var data = characteristic.value
      var reportData = data.bytes
      var bpm : UInt16
      var rawByte : UInt8
      var outputString = ""
      rawByte = UInt8(reportData[0])
      bpm = 0
  
      if ((rawByte &amp; 0x01) == 0) {          // 2
          // Retrieve the BPM value for the Heart Rate Monitor
          bpm = UInt16( reportData[4] )
      }
      else {
          bpm = CFSwapInt16LittleToHost(UInt16(reportData[1]))
      }
  
      outputString = String(bpm)
      return outputString
  }
  </code></pre>