Swift - converting from ConstUnsafePointer<()>

ios, swift

Solution

What you were looking for was how to convert NSData to an array of UInt8. Here's how.

import Foundation
let path = "/etc/csh.cshrc" // something existent
let data = NSData(contentsOfFile: path)

var aofb = [UInt8](count:data.length, repeatedValue:0)
data.getBytes(&aofb, length:data.length)

for c in aofb {
    let s = UnicodeScalar(Int(c)).escape(asASCII:true)
    println("\(c):\(s)")
}

Problem

I'm on beta 3. Consider the following Objective-C line: ``` const uint8_t *reportData = [data bytes]; ``` where `data` is a `NSData` object. How would this line be re-written in Swift? `data.bytes` is of type `ConstUnsafePointer<()>`, and while there's plenty of documentation on how to create a pointer type in Swift, there isn't much info on how to work with them. edit: To add some context, I'm trying to port Apple's HeartRateMonitor sample code to Swift. This code interacts with BLE heart rate monitors. This code I'm working on translates the data received by the Bluetooth system into an int for use in the UI. The data received from BT is expected to be an array of uints, element 0 is used to check for a flag and element 1 contains the value. Here's the same Objective-C line in context: ``` const uint8_t *reportData = [data bytes]; uint16_t bpm = 0; if ((reportData[0] & 0x01) == 0) { /* uint8 bpm */ bpm = reportData[1]; } ```

Original source