mownier
mownier

Reputation: 1779

UnsafeMutablePointer initialization

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

Using Swift 3, line 3 produces an error that says:

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

Is there a way to convert UnsafeMutableRawPointer to any appropriate type that UnsafeMutablePointer accepts as a parameter upon initialization?

By the way, the class reference for UnsafeMutablePointer can be found here.

Upvotes: 1

Views: 471

Answers (1)

mownier
mownier

Reputation: 1779

I guess this will do the initialization.

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

Upvotes: 1

Related Questions