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

# withUnsafeBytes(_:)

Call a function and pass a buffer representing the value of this
instance’s [`attachableValue`](/documentation/Testing/Attachment/attachableValue-2tnj5) property to it.

```
borrowing func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) throws -> R
```

## Parameters

`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. This
function calls the [`withUnsafeBytes(for:_:)`](/documentation/Testing/Attachable/withUnsafeBytes(for:_:)) function on this
attachment’s [`attachableValue`](/documentation/Testing/Attachment/attachableValue-2tnj5) property.

---

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)