• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why is NSString.UTF8String unavailable in Swift?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is NSString.UTF8String unavailable in Swift?


  • Subject: Re: Why is NSString.UTF8String unavailable in Swift?
  • From: Greg Parker <email@hidden>
  • Date: Mon, 23 Mar 2015 13:52:21 -0700

On Mar 21, 2015, at 8:59 PM, Quincey Morris <email@hidden> wrote:
> On Mar 21, 2015, at 20:43 , Charles Srstka <email@hidden> wrote:
>> 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).
>
> What’s interesting about that is (assuming my playground test is correct) the native Swift String type only “acquires” cStringUsingEncoding when Cocoa is imported.
>
> That means either:
>
> a. The native class actually has that method, but doesn’t expose it to the world without “import Cocoa”, OR
>
> b. The native class must implement some kind of selector forwarding that translates that method into a message to a real NSString conversion of the String instance, or some such roundabout thing.
>
> Option (a) seems strange, but option (b) seems even stranger.

c. None of the above.

When you `import Cocoa` in Swift code you get some Swift additions as well as Cocoa's Objective-C declarations.

Most of NSString's methods are re-implemented in a Swift extension on class String. You get this extension when you `import Cocoa`.

UTF8String was deliberately omitted from the extension on class String because String already has a `utf8` property. If you think String.UTF8String should be supported then you should file a bug report.


--
Greg Parker     email@hidden     Runtime Wrangler



_______________________________________________

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


References: 
 >Why is NSString.UTF8String unavailable in Swift? (From: Jens Alfke <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Quincey Morris <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Jens Alfke <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Quincey Morris <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Charles Srstka <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Quincey Morris <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Charles Srstka <email@hidden>)
 >Re: Why is NSString.UTF8String unavailable in Swift? (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: Swapping views in print panel
  • Next by Date: Re: Memory optimization of NSAttributedString
  • Previous by thread: Re: Why is NSString.UTF8String unavailable in Swift?
  • Next by thread: Re: Why is NSString.UTF8String unavailable in Swift?
  • Index(es):
    • Date
    • Thread