J.doouuu J.doouuu - 2 months ago 203
iOS Question

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

I'm trying to update my project to swift 3.0, and I'm having trouble in the UnsafeMutablePointer.

here is my error

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


code:

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


why? How to fix it?




and nearly,here is another error, they looked quite similar:

'init' is unavailable:use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.


here is my code:

let xAsComplex = UnsafePointer<DSPComplex>( inputSignal.withUnsafeBufferPointer { $0.baseAddress } )


why? How to fix it?

Answer

Converting from void pointer (aka UnsafeMutableRawPointer) has changed in Swift 3. You have 2 options:

If you know your buffer length (safer):

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

If you don't know it:

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