A view of a string’s contents as a collection of UTF-8 code units.
SDK
- Xcode 6.0.1+
Framework
- Swift Standard Library
Declaration
Overview
You can access a string’s view of UTF-8 code units by using its utf8
property. A string’s UTF-8 view encodes the string’s Unicode scalar values as 8-bit integers.
A string’s Unicode scalar values can be up to 21 bits in length. To represent those scalar values using 8-bit integers, more than one UTF-8 code unit is often required.
In the encoded representation of a Unicode scalar value, each UTF-8 code unit after the first is called a continuation byte.
UTF8View Elements Match Encoded C Strings
Swift streamlines interoperation with C string APIs by letting you pass a String
instance to a function as an Int8
or UInt8
pointer. When you call a C function using a String
, Swift automatically creates a buffer of UTF-8 code units and passes a pointer to that buffer. The code units of that buffer match the code units in the string’s utf8
view.
The following example uses the C strncmp
function to compare the beginning of two Swift strings. The strncmp
function takes two const char*
pointers and an integer specifying the number of characters to compare. Because the strings are identical up to the 14th character, comparing only those characters results in a return value of 0
.
Extending the compared character count to 15 includes the differing characters, so a nonzero result is returned.