Instance Property

utf8CString

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

Declaration

var utf8CString: ContiguousArray<CChar> { get }

Discussion

To access the underlying memory, invoke withUnsafeBufferPointer on the array.

let s = "Hello!"
let bytes = s.utf8CString
print(bytes)
// Prints "[72, 101, 108, 108, 111, 33, 0]"

bytes.withUnsafeBufferPointer { ptr in
    print(strlen(ptr.baseAddress!))
}
// Prints "6"

See Also

Getting C Strings

func withCString<Result>((UnsafePointer<Int8>) -> Result) -> Result

Calls the given closure with a pointer to the contents of the string, represented as a null-terminated sequence of UTF-8 code units.

func withCString<Result, TargetEncoding>(encodedAs: TargetEncoding.Type, (UnsafePointer<TargetEncoding.CodeUnit>) -> Result) -> Result

Calls the given closure with a pointer to the contents of the string, represented as a null-terminated sequence of code units.