Subscript

subscript(_:)

Accesses the code unit at the given position.

Declaration

subscript(i: String.UTF8View.Index) -> UTF8.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-8 code unit.

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

Relationships