Swift NSData Initialiser

时间:2015-07-07 03:02:56

标签: objective-c swift nsdata

我想从Obj-C库中实现这个方法。

func bleDidReceiveData(data: UnsafeMutablePointer<UInt8>, length: Int32) {
    var d = NSData(bytes: data as [UInt8], length: length)
}

然而,当我尝试初始化NSData时,它给了我这个错误

Cannot find an initialiser for type NSData that accepts an argument of type (bytes:[UInt8],length:int32)

在Obj-C中,这是以这种方式完成的

NSData *d = [NSData dataWithBytes:data length:length];

1 个答案:

答案 0 :(得分:0)

func bleDidReceiveData(data: UnsafeMutablePointer<UInt8>, length: Int32) {
    var d = NSData(bytes: UnsafeMutablePointer<Void>(data), length: Int(length))
}