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
Unmanaged
value, usefromOpaque
. UnlikepassRetained
andpassUnretained
, you must specify the generic type forUnmanaged
when usingfromOpaque
so that the compiler knows which class you're expecting to receive.Once you have the
Unmanaged
value, you can get a reference out of it. UsingtakeRetainedValue
will perform an unbalanced release, which will balance an unbalanced retain previously performed withpassRetained
, or by C code which confers ownership on your code. UsingtakeUnretainedValue
will obtain the object reference without performing any retain or release.[…]
To retrieve a reference from C into Swift code, you create an
Unmanaged
value 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.