8

How does one get a char/CChar in Swift? I need to call the following method:

- (void)registerOption:(NSString *)longOption shortcut:(char)shortOption requirement:(GBValueRequirements)requirement;

I can pass the ASCII form of the char, but that is annoying. Is there a simple way to get the char at a certain index in a string?

Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
Josh The Geek
  • 1,203
  • 12
  • 24
  • possible duplicate of [How do you convert a String to a CString in the Swift Language?](http://stackoverflow.com/questions/24103590/how-do-you-convert-a-string-to-a-cstring-in-the-swift-language) – Jack Jun 23 '14 at 16:26
  • Not a `CString`, a `CChar` – Josh The Geek Jun 23 '14 at 16:32
  • What's the difference? a C string is a `char *` – Jack Jun 23 '14 at 16:33
  • 1
    And a `CChar` is a `char`, without the pointer. A `char` is one character (one byte), but a `char *` is an array of characters, a string (https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html) – Josh The Geek Jun 23 '14 at 16:38
  • @JoshTheGeek Can you not take the `CString` (`char *`) and take the first `char` in the array? – erdekhayser Jun 23 '14 at 17:39

4 Answers4

9

You can convert a Swift string to a Cstring and then just grab the first (and only) character:

var charString = "a"
var ccharOptional = charString.cStringUsingEncoding(NSUTF8StringEncoding)?[0]  // CChar?
var cchar = (charString.cStringUsingEncoding(NSUTF8StringEncoding)?[0])!       // CChar
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
3

Using Swift 3.0:

let cchar = "a".utf8CString[0]

(not sure when this was actually introduced, tho)

Mazyod
  • 22,319
  • 10
  • 92
  • 157
2

With Swift 5, you can use one of the two following ways in order to get a collection of CChar from a String instance.


#1. Using Swift's utf8CString property

String has a utf8CString property. utf8CString has the following declaration:

var utf8CString: ContiguousArray<CChar> { get }

A contiguously stored null-terminated UTF-8 representation of the string.

The Playground sample code below shows how to use utf8CString:

let string = "Café "
let bytes = string.utf8CString
print(bytes) // prints: [67, 97, 102, -61, -87, 32, -16, -97, -111, -115, 0]

#2. Using StringProtocol's cString(using:) method

Foundation provides String a cString(using:) method. cString(using:) has the following declaration:

func cString(using encoding: String.Encoding) -> [CChar]?

Returns a representation of the string as a C string using a given encoding.

The Playground sample code below shows how to get a collection of CChar from an string using cString(using:):

import Foundation

let string = "Café "
let bytes = string.cString(using: String.Encoding.utf8)
print(bytes) // prints: Optional([67, 97, 102, -61, -87, 32, -16, -97, -111, -115, 0])
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
0

In swift3 (3.0.2): let cchar = "abc".cString(using: .utf8) returns array of CChar.