<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Unicode/UTF8/width(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s7UnicodeO4UTF8O5widthySiAB6ScalarVFZ"
  },
  "title" : "width(_:)"
}
-->

# width(_:)

Returns the number of code units required to encode the given Unicode
scalar.

```
static func width(_ x: Unicode.Scalar) -> Int
```

## Parameters

`x`

A Unicode scalar value.

## Return Value

The width of `x` when encoded in UTF-8, from `1` to `4`.

## Discussion

Because a Unicode scalar value can require up to 21 bits to store its
value, some Unicode scalars are represented in UTF-8 by a sequence of up
to 4 code units. The first code unit is designated a *lead* byte and the
rest are *continuation* bytes.

```
let anA: Unicode.Scalar = "A"
print(anA.value)
// Prints "65"
print(UTF8.width(anA))
// Prints "1"

let anApple: Unicode.Scalar = "🍎"
print(anApple.value)
// Prints "127822"
print(UTF8.width(anApple))
// Prints "4"
```

---

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)