---
title: "withUnsafeBytes(for:_:)"
framework: testing
role: symbol
role_heading: Instance Method
path: "testing/attachable/withunsafebytes(for:_:)"
---

# withUnsafeBytes(for:_:)

Call a function and pass a buffer representing this instance to it.

## Declaration

```swift
borrowing func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R
```

## Parameters

- `attachment`: The attachment that is requesting a buffer (that is, the attachment containing this instance.)
- `body`: A function to call. A temporary buffer containing a data representation of this instance is passed to it.

## Mentioned in

Migrating a test from XCTest

## Return Value

Return Value Whatever is returned by body.

## Discussion

Discussion note: Whatever is thrown by body, or any error that prevented the creation of the buffer. The testing library uses this function when saving an attachment. The format of the buffer is implementation-defined, but should be “idiomatic” for this type: for example, if this type represents an image, it would be appropriate for the buffer to contain an image in PNG format, JPEG format, etc., but it would not be idiomatic for the buffer to contain a textual description of the image.
