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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to