• 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: Quincey Morris <email@hidden>
  • Date: Sat, 21 Mar 2015 23:58:07 +0000

On Mar 21, 2015, at 16:41 , Jens Alfke <email@hidden> wrote:
>
> Sure, but it’s bridged with NSString. The “Using Swift With Cocoa” book says: “Swift automatically bridges between the String type and the NSString class. This means that anywhere you use an NSString object, you can use a Swift String type instead  … you should almost never need to use the NSString class directly in your own code.”

Yes, but there’s there’s no opportunity for bridging in your code. Bridging means that when String is used where NSString is expected, or vice versa, there’s an implicit type conversion. The variable was of type “String”, used in a context where String was expected, and type String has no “UTF8String” property. No appeal to NSString was made or implied by your code — there’s no reason why a random method/property name should be looked up in any other class.

> -cStringUsingEncoding: and several other NSString methods I’ve tried work without the “as NSString” trick; what’s special about “UTF8String”?


The difference is mundane: String happens to have a “cStringUsingEncoding” method, but not a “UTF8String” method. NSString happens to have both. I don’t know why — though I can’t imagine it’s merely been overlooked in a class as common as String — but that difference is the reason for the error message.

When you used “cStringUsingEncoding”, you weren’t bridging to the NSString method, you were using String’s native method of the same name. It’s possible that String’s cStringUsingEncoding is internally calling through to NSString’s, but I wouldn’t bet on it.




_______________________________________________

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


  • Follow-Ups:
    • Re: Why is NSString.UTF8String unavailable in Swift?
      • From: Charles Srstka <email@hidden>
    • Re: Why is NSString.UTF8String unavailable in Swift?
      • From: Jens Alfke <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>)

  • Prev by Date: Re: Why is NSString.UTF8String unavailable in Swift?
  • Next by Date: Re: Why is NSString.UTF8String unavailable in Swift?
  • 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