Why is NSString.UTF8String unavailable in Swift?
Why is NSString.UTF8String unavailable in Swift?
- Subject: Why is NSString.UTF8String unavailable in Swift?
- From: Jens Alfke <email@hidden>
- Date: Sat, 21 Mar 2015 14:02:15 -0700
I’m writing Swift code to call a C function that takes a string in the form of a char*. The value I have is a String, obviously. I thought this would work:
import Foundation
let s = “……”
some_function(s.UTF8String)
but this fails to compile, with an error “‘String’ does not have a member ‘UTF8String’”. Which is weird, because if I open the auto-translated Swift version of NSString.h, it’s in there:
var UTF8String: UnsafePointer<Int8> { get }
I finally worked out that I could use
s.cStringUsingEncoding(NSUTF8StringEncoding)
but this is kind of awkward, and I’m curious why UTF8String doesn’t work. Anyone know why?
You can try this yourself in a playground containing this text:
import Cocoa
var str = "Hello, playground"
str.cStringUsingEncoding(NSUTF8StringEncoding) // OK
str.UTF8String // <— ‘String’ does not have a member ‘UTF8String’
—Jens
PS: This is with the latest Xcode 6.3 beta.
_______________________________________________
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