Home > Enterprise >  How to convert array of strings in swift to UnsafeMutableRawPointer?
How to convert array of strings in swift to UnsafeMutableRawPointer?

Time:05-01

Example code: let context = ["ABC", "XYZ", nil]

How to convert this array to type UnsafeMutableRawPointer?

CodePudding user response:

You can withUnsafeMutablePointer to get a typed unsafe pointer and then create a raw version if you need it untyped:

var context = ["ABC", "XYZ", nil]
withUnsafeMutablePointer(to: &context) { p in
    let rawPointer = UnsafeMutableRawPointer(p)
    print(rawPointer)
}

Of course, beware of the memory issues that come along with using any of the 'unsafe' Swift features.

  • Related