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".


No comments:

Post a Comment