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: 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