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"