<!--
{
  "availability" : [
    "Swift: 6.2.0 -",
    "Xcode: 26.0.0 -",
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "swift: 6.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/Attachable/withUnsafeBytes(for:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift Testing"
    ],
    "preciseIdentifier" : "s:7Testing10AttachableP15withUnsafeBytes3for_qd__AA10AttachmentVyxG_qd__SWKXEtKlF"
  },
  "title" : "withUnsafeBytes(for:_:)"
}
-->

# withUnsafeBytes(for:_:)

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

```
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.

## Return Value

Whatever is returned by `body`.

## Discussion> Throws: 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.

---

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)