• 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
Why is NSString.UTF8String unavailable in Swift?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Why is NSString.UTF8String unavailable in Swift?
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Cleaning up a project from static/global variables
  • Next by Date: Re: Why is NSString.UTF8String unavailable in Swift?
  • Previous by thread: Scrolling while resizing a UITableView with UITextFields
  • Next by thread: Re: Why is NSString.UTF8String unavailable in Swift?
  • Index(es):
    • Date
    • Thread