<!--
{
  "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/StaticString/withUTF8Buffer(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s12StaticStringV14withUTF8BufferyxxSRys5UInt8VGXElF"
  },
  "title" : "withUTF8Buffer(_:)"
}
-->

# withUTF8Buffer(_:)

Invokes the given closure with a buffer containing the static string’s
UTF-8 code unit sequence (excluding the null terminator).

```
func withUTF8Buffer<R>(_ body: (UnsafeBufferPointer<UInt8>) -> R) -> R
```

## Parameters

`body`

A closure that takes a buffer pointer to the static
string’s UTF-8 code unit sequence as its sole argument. If the closure
has a return value, that value is also used as the return value of the
`withUTF8Buffer(_:)` method. The pointer argument is valid only for the
duration of the method’s execution.

## Return Value

The return value, if any, of the `body` closure.

## Discussion

This method works regardless of whether the static string stores a
pointer or a single Unicode scalar value.

The pointer argument to `body` is valid only during the execution of
`withUTF8Buffer(_:)`. Do not store or return the pointer for later use.

---

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)