<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UTF8Span",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s8UTF8SpanV"
  },
  "title" : "UTF8Span"
}
-->

# UTF8Span

A borrowed view into contiguous memory that contains validly-encoded UTF-8 code units.

```
@frozen struct UTF8Span
```

## Topics

### Structures

[`struct CharacterIterator`](/documentation/Swift/UTF8Span/CharacterIterator)

Iterate the `Character` contents of a `UTF8Span`.

[`struct UnicodeScalarIterator`](/documentation/Swift/UTF8Span/UnicodeScalarIterator)

Iterate the `Unicode.Scalar`s contents of a `UTF8Span`.

### Initializers

[`init(unchecked: Span<UInt8>, isKnownASCII: Bool)`](/documentation/Swift/UTF8Span/init(unchecked:isKnownASCII:))

Creates a UTF8Span, bypassing safety and security checks. The caller
must guarantee that `codeUnits` contains validly-encoded UTF-8, or else
undefined behavior may result upon use. If `isKnownASCII: true is passed`, the contents must be ASCII, or else undefined behavior may
result upon use.

[`init(validating: consuming Span<UInt8>) throws(UTF8.ValidationError)`](/documentation/Swift/UTF8Span/init(validating:))

Creates a UTF8Span containing `codeUnits`. Validates that the input is
valid UTF-8, otherwise throws an error.

### Instance Properties

[`var count: Int`](/documentation/Swift/UTF8Span/count)

The number of UTF-8 code units in the span.

[`var isEmpty: Bool`](/documentation/Swift/UTF8Span/isEmpty)

A Boolean value that indicates whether the UTF-8 span is empty.

[`var isKnownASCII: Bool`](/documentation/Swift/UTF8Span/isKnownASCII)

Returns whether contents are known to be all-ASCII. A return value of
`true` means that all code units are ASCII. A return value of `false`
means there *may* be non-ASCII content.

[`var isKnownNFC: Bool`](/documentation/Swift/UTF8Span/isKnownNFC)

Returns whether the contents are known to be NFC. This is not
always checked at initialization time and is set by `checkForNFC`.

[`var span: Span<UInt8>`](/documentation/Swift/UTF8Span/span)

A span used to access the code units.

### Instance Methods

[`func bytesEqual(to: some Sequence<UInt8>) -> Bool`](/documentation/Swift/UTF8Span/bytesEqual(to:))

Whether this span has the same bytes as `other`.

[`func charactersEqual(to: some Sequence<Character>) -> Bool`](/documentation/Swift/UTF8Span/charactersEqual(to:))

Whether this span has the same `Character`s as `other`.

[`func checkForASCII() -> Bool`](/documentation/Swift/UTF8Span/checkForASCII())

Do a scan checking for whether the contents are all-ASCII.

[`func checkForNFC(quickCheck: Bool) -> Bool`](/documentation/Swift/UTF8Span/checkForNFC(quickCheck:))

Do a scan checking for whether the contents are in Normal Form C.
When the contents are in NFC, canonical equivalence checks are much
faster.

[`func isCanonicallyEquivalent(to: UTF8Span) -> Bool`](/documentation/Swift/UTF8Span/isCanonicallyEquivalent(to:))

Whether `self` is equivalent to `other` under Unicode Canonical
Equivalence.

[`func isCanonicallyLessThan(UTF8Span) -> Bool`](/documentation/Swift/UTF8Span/isCanonicallyLessThan(_:))

Whether `self` orders less than `other` under Unicode Canonical
Equivalence using normalized code-unit order (in NFC).

[`func isTriviallyIdentical(to: UTF8Span) -> Bool`](/documentation/Swift/UTF8Span/isTriviallyIdentical(to:))

Returns a Boolean value indicating whether two instances refer to the same
memory region, and have the same flags (such as [`isKnownASCII`](/documentation/Swift/UTF8Span/isKnownASCII)).

[`func makeCharacterIterator() -> UTF8Span.CharacterIterator`](/documentation/Swift/UTF8Span/makeCharacterIterator())

Returns an iterator that will construct `Character`s from the underlying
UTF-8 content.

[`func makeUnicodeScalarIterator() -> UTF8Span.UnicodeScalarIterator`](/documentation/Swift/UTF8Span/makeUnicodeScalarIterator())

Returns an iterator that will decode the code units into
`Unicode.Scalar`s.

[`func unicodeScalarsEqual(to: some Sequence<Unicode.Scalar>) -> Bool`](/documentation/Swift/UTF8Span/unicodeScalarsEqual(to:))

Whether this span has the same `Unicode.Scalar`s as `other`.

## Relationships

### Conforms To

[`ContiguousBytes`](/documentation/Foundation/ContiguousBytes)

[`BitwiseCopyable`](/documentation/Swift/BitwiseCopyable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)