Random code snippets and bits of programming related wisdom that I have found useful.
Tuesday, December 6, 2016
Converting a swift object to a pointer (and back)
Sometimes it is necessary to convert a swift object to a pointer. This is required in situations, for example, where you need to pass a swift object to a C function that takes a void * (AKA UnsafeMutableRawPointer) parameter. Getting a pointer to a swift class is fairly simple, although it can take a while to find the correct syntax. The following code snipper will convert a Swift NSArray instance to an UnsafeMutableRawPointer, and back to a NSArray without changing the retain count. If you paste the code into a playground, it will print "same array".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
No comments:
Post a Comment