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

Kenny Leung kenny_leung at pobox.com
Wed Jun 22 14:02:57 CDT 2016


Thanks! Missed that.

I think it would be OK if the transparent bridging called cString(using:) itself, and the whole thing would fail if the conversion fails.

-Kenny


> On Jun 22, 2016, at 11:52 AM, Ben Rimmington <me at benrimmington.com> wrote:
> 
> 
>> 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