Subscript

subscript(_:)

Accesses the code unit at the given position.

Declaration

subscript(idx: String.UTF16View.Index) -> UTF16.CodeUnit { get }

Parameters

position

A valid index of the view. position must be less than the view’s end index.

Discussion

The following example uses the subscript to print the value of a string’s first UTF-16 code unit.

let greeting = "Hello, friend!"
let i = greeting.utf16.startIndex
print("First character's UTF-16 code unit: \(greeting.utf16[i])")
// Prints "First character's UTF-16 code unit: 72"

Relationships