Re: Why is NSString.UTF8String unavailable in Swift?
Re: Why is NSString.UTF8String unavailable in Swift?
- Subject: Re: Why is NSString.UTF8String unavailable in Swift?
- From: Charles Srstka <email@hidden>
- Date: Sat, 21 Mar 2015 22:43:04 -0500
> On Mar 21, 2015, at 9:28 PM, Quincey Morris <email@hidden> wrote:
>
> On Mar 21, 2015, at 18:55 , Charles Srstka <email@hidden <mailto:email@hidden>> wrote:
>>
>> The implicit conversions between String and NSString were removed in Swift 1.2
>
> I’m sorry, I muddied the waters by using an incorrect description of “bridging” earlier.
>
> Bridging in Obj-C is something like NSString vs CFString. The types and their APIs are formally distinct at compile time, even though the objects might be interchangeable at run time. If you use the “wrong” API for your declared type, you’ll get a compiler error, though the semantics of what you wrote might be sensible in terms of run-time behavior.
>
> I think it’s the same thing in Swift: String and NSString are distinct types, and although their APIs almost exactly coincide, they’re not identical. (We found that UTF8String, for example, is in NSString but not String.) That implies the possibility of compile-time errors for things that might otherwise be meaningful in run-time terms. That’s what happened to Jens, I think.
>
> However, I don’t think the level of run-time interoperability is exactly specified by API contract. It may be that there’s a shared toll-free bridged String/NSString object, or it may be that there are run-time conversions from “real” String objects to “real” NSString objects under some circumstances. It’s not obvious which of the two it is, though someone who knows more about Swift’s run-time than I do may be able to answer that definitively.
I’m pretty sure that “real” Swift strings are different from NSStrings at runtime. It’s not like NSString/CFString; it has to do an actual conversion, and is not toll-free bridged. If you convert between String and NSString a lot, it’ll have performance implications (which is why bridging to NSString just to get -UTF8String isn’t really a good idea).
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden