UnsafeMutablePointer初始化

时间:2016-10-05 04:00:36

标签: ios swift3

1. let context = CGContext(...)
2. context.draw(...)
3. let buffer = UnsafeMutablePointer<UInt32>(context.data) // error here  

使用Swift 3,第3行会产生一个错误:

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

有没有办法将UnsafeMutableRawPointer转换为UnsafeMutablePointer在初始化时作为参数接受的任何适当类型?

顺便说一句,UnsafeMutablePointer的类引用可以找到here

1 个答案:

答案 0 :(得分:0)

我想这会进行初始化。

    let ptr = context.data
    let data = ptr!.assumingMemoryBound(to: UnsafeMutablePointer<UInt32>.self).pointee
    let pixelBuffer = UnsafeMutablePointer<UInt32>(data)