Cast a Swift struct to UnsafeMutablePointer<Void> Cast a Swift struct to UnsafeMutablePointer<Void> swift swift

Cast a Swift struct to UnsafeMutablePointer<Void>


As far as I know, the shortest way is:

var myStruct = TheStruct()var address = withUnsafeMutablePointer(&myStruct) {UnsafeMutablePointer<Void>($0)}

But, why you need this? If you want pass it as a parameter, you can (and should):

func foo(arg:UnsafeMutablePointer<Void>) {    //...}var myStruct = TheStruct()foo(&myStruct)


Most of the method prototypes have been changed as Swift evolved over the years. Here is the syntax for Swift 5:

    var struct = TheStruct()    var unsafeMutablePtrToStruct = withUnsafeMutablePointer(to: &struct) {        $0.withMemoryRebound(to: TheStruct.self, capacity: 1) {            (unsafePointer: UnsafeMutablePointer<TheStruct>) in            unsafePointer        }    }