Creates an index in the given UTF-8 view that corresponds exactly to the specified
- Xcode 9.0+
- Swift Standard Library
A position in a
Stringor one of its views.
UTF8Viewin which to find the new position.
The following example finds the position of a space in a string’s
utf16 view and then converts that position to an index in the string’s
If the position passed in
utf16Index doesn’t have an exact corresponding position in
utf8, the result of the initializer is
nil. For example, because UTF-8 and UTF-16 represent high Unicode code points differently, an attempt to convert the position of the trailing surrogate of a UTF-16 surrogate pair fails.
The next example attempts to convert the indices of the two UTF-16 code points that represent the teacup emoji (
"🍵"). The index of the lead surrogate is successfully converted to a position in
utf8, but the index of the trailing surrogate is not.