Swift.Unmanaged
In order to work with C APIs, we sometimes need to convert Swift object references to and from raw pointers. […] You can do this with
unsafeBitCast, although I strongly recommend against it.[…]
To create the
Unmanagedvalue, usefromOpaque. UnlikepassRetainedandpassUnretained, you must specify the generic type forUnmanagedwhen usingfromOpaqueso that the compiler knows which class you're expecting to receive.Once you have the
Unmanagedvalue, you can get a reference out of it. UsingtakeRetainedValuewill perform an unbalanced release, which will balance an unbalanced retain previously performed withpassRetained, or by C code which confers ownership on your code. UsingtakeUnretainedValuewill obtain the object reference without performing any retain or release.[…]
To retrieve a reference from C into Swift code, you create an
Unmanagedvalue from the raw pointer, then take a reference from it. There are two ways to take a reference: one which consumes an unbalanced retain, and once which performs no memory management.
Previously: Swift 3.0 Unsafe World.