Type Method

isLeadSurrogate(_:)

Returns a Boolean value indicating whether the specified code unit is a high-surrogate code unit.

Declaration

static func isLeadSurrogate(_ x: Unicode.UTF16.CodeUnit) -> Bool

Parameters

x

A UTF-16 code unit.

Return Value

true if x is a high-surrogate code unit; otherwise, false.

Discussion

Here’s an example of checking whether each code unit in a string’s utf16 view is a lead surrogate. The apple string contains a single emoji character made up of a surrogate pair when encoded in UTF-16.

let apple = "🍎"
for unit in apple.utf16 {
    print(UTF16.isLeadSurrogate(unit))
}
// Prints "true"
// Prints "false"

This method does not validate the encoding of a UTF-16 sequence beyond the specified code unit. Specifically, it does not validate that a low-surrogate code unit follows x.