[swift-evolution] [Pitch] Remove transparent bridging between Swift strings and char *

Ben Rimmington me at benrimmington.com
Wed Jun 22 13:52:38 CDT 2016

> On 22 Jun 2016, at 17:37, Kenny Leung via swift-evolution <swift-evolution at swift.org> wrote:
> What does surprise me is that Swift String bridges directly into “char *” arguments in C as nul-terminated C strings, apparently preserving unicode and all. I can find nothing on bridging to “char *” in “Using Swift with Cocoa and Objective-C"

* Using Swift with Cocoa and Objective-C > Interoperability > Interacting with C APIs > Pointers > Constant Pointers:

"When a function is declared as taking a UnsafePointer<Type> argument, it can accept [...] A String value, if Type is Int8 or UInt8. The string will automatically be converted to UTF8 in a buffer, and a pointer to that buffer is passed to the function."

> In the spirit of preventing you from hurting yourself, I think this functionality should be removed, forcing you to use cString(using:) first.

Do you mean the encoding should always be given, instead of using UTF-8 by default? I think the no-argument -[NSString cString] method was deprecated for this reason?

-- Ben

More information about the swift-evolution mailing list