无法为类型&#39; UnsafeMutablePointer&#39; <uint8>&#39;调用初始值设定项。使用类型&#39;(UnsafeMuatableRawpointer?)&#39;的参数列表?

时间:2016-09-18 16:42:43

标签: ios swift

我试图将我的项目更新为swift 3.0,并且我在UnsafeMutablePointer中遇到了麻烦。

这是我的错误

Cannot invoke initializer for type 'UnsafeMutablePointer<uint8>' with an argument list of type '(UnsafeMuatableRawpointer?)'

代码:

let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let dataBuffer = UnsafeMutablePointer<UInt8>(src_buff)    //error here

为什么呢?如何解决?

1 个答案:

答案 0 :(得分:3)

在Swift 3中,从void指针(又名UnsafeMutableRawPointer)转换已更改。您有2个选项:

如果您知道缓冲区长度(更安全):

let dataBuffer = src_buff?.bindMemory(to: UInt8.self, capacity: len)

如果您不知道:

let dataBuffer = src_buff?.assumingMemoryBound(to: UInt8.self)