无法在 Swift 中创建 NSData?

Unable to create NSData in Swift?

求教,这是怎么回事?

let indices: [UInt8] = [ 0, 2, 1 ]
let indexData: NSData = NSData(bytes:indices as UInt8, length: sizeof(indices))

我正在使用 Xcode 7(测试版)/Swift 2.0

不断运行进入: 找不到类型 'NSData' 的初始值设定项,它接受类型为“(bytes:int, length:int)”

的参数列表

任何见解表示赞赏。

下面是如何在字节数组和 NSData 之间来回切换。

// Works in a playground as is

// Convert [UInt8] to NSData
let indices: [UInt8] = [ 0, 2, 1 ]
let indexData: NSData = NSData(bytes:indices, length: indices.count)

// Get [UInt8] from NSData
var bytes = [UInt8](count: indexData.length, repeatedValue: 0)
indexData.getBytes(&bytes, length: indexData.length)
print(bytes)