---
title: "withUnsafeBufferPointer(_:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/span/withunsafebufferpointer(_:)"
---

# withUnsafeBufferPointer(_:)

Calls a closure with a pointer to the viewed contiguous storage.

## Declaration

```swift
func withUnsafeBufferPointer<E, Result>(_ body: (UnsafeBufferPointer<Element>) throws(E) -> Result) throws(E) -> Result where E : Error, Result : ~Copyable
```

## Parameters

- `body`: A closure with an UnsafeBufferPointer parameter that points to the viewed contiguous storage. If body has a return value, that value is also used as the return value for the withUnsafeBufferPointer(_:) method. The closure’s parameter is valid only for the duration of its execution.

## Return Value

Return Value The return value of the body closure parameter.

## Discussion

Discussion The buffer pointer passed as an argument to body is valid only during the execution of withUnsafeBufferPointer(_:). Do not store or return the pointer for later use. Note: For an empty Span, the closure always receives a nil pointer.
