CVarArg support for NSString and String

I created PR #2821 to add CVarArg support to NSString and String.

This is something I use on my Android app for localized strings, which are shared from the iOS version. It has also been reported as SR-957.

Adding support to NSString was actually easy, since CoreFoundation already supports the "%@" format. All that was required was adding an extension that implements the CVarArg protocol by returning the CFString instance encoded as bits.

However, String support is messy. The CFString requires the String be wrapped by an NSString. This needs to be retained long enough for the withVaList closure to complete.

This could be solved by using an autorelease pool, but that isn't implemented on non-Apple platforms. There was some discussion in the forums about this a few years ago, but no resolution.

My solution was to schedule the release using an OperationQueue operation. I'm not crazy about this, but it works and is certainly better than the current broken state.

Technically, all of the pointer extensions in VarArgs.swift could suffer from dangling pointers.

Any other ideas on how this could be done?

To avoid the hacky OperationQueue operation, I added a new _CVarArgObject protocol to retain the NSString. This is found in PR #32311.

Terms of Service

Privacy Policy

Cookie Policy