If we define a C function that returns a CFType
, Swift can (sometimes) bridge the memory management semantics. If Swift cannot bridge the memory management semantics, we can end up with an Unmanaged
type:
CFStringRef StringByAddingTwoStrings(CFStringRef s1, CFStringRef s2);
bridges to:
public func StringByAddingTwoStrings(_ s1: CFString!, _ s2: CFString!) -> Unmanaged<CFString>!
We can improve the bridging with the CF_RETURNS_RETAINED
annotation:
CFStringRef StringByAddingTwoStrings(CFStringRef s1, CFStringRef s2) CF_RETURNS_RETAINED;
now bridges to:
public func StringByAddingTwoStrings(_ s1: CFString!, _ s2: CFString!) -> CFString!
What does not seem to be documented is how to improve bridging when function pointer return values should be annotated. For example:
typedef CFStringRef (*StringByAddingTwoStrings)(CFStringRef, CFStringRef);
bridges to:
public typealias StringByAddingTwoStrings = @convention(c) (CFString?, CFString?) -> Unmanaged<CFString>?
Is there any way to annotate the function pointer to properly communicate the memory management semantics? Adding the CF_RETURNS_RETAINED
to the function pointer does not seem to help.