<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift Testing"
    ],
    "preciseIdentifier" : "s:7Testing10AttachableP"
  },
  "title" : "Attachable"
}
-->

# Attachable

A protocol describing a type whose instances can be recorded and saved as
part of a test run.

```
protocol Attachable : ~Copyable
```

## Overview

To attach an attachable value to a test, pass it to [`record(_:named:sourceLocation:)`](/documentation/Testing/Attachment/record(_:named:sourceLocation:)).
To further configure an attachable value before you attach it, use it to
initialize an instance of [`Attachment`](/documentation/Testing/Attachment) and set its properties before
passing it to [`record(_:sourceLocation:)`](/documentation/Testing/Attachment/record(_:sourceLocation:)).

The testing library provides default conformances to this protocol for a
variety of standard library types. Most user-defined types do not need to
conform to this protocol.

A type should conform to this protocol if it can be represented as a
sequence of bytes that would be diagnostically useful if a test fails. If a
type cannot conform directly to this protocol (such as a non-final class or
a type declared in a third-party module), you can create a wrapper type that
conforms to [`AttachableWrapper`](/documentation/Testing/AttachableWrapper) to act as a proxy.

## Topics

### Instance Properties

[`var estimatedAttachmentByteCount: Int?`](/documentation/Testing/Attachable/estimatedAttachmentByteCount)

An estimate of the number of bytes of memory needed to store this value as
an attachment.

### Instance Methods

[`func preferredName(for: borrowing Attachment<Self>, basedOn: String) -> String`](/documentation/Testing/Attachable/preferredName(for:basedOn:))

Generate a preferred name for the given attachment.

[`func withUnsafeBytes<R>(for: borrowing Attachment<Self>, (UnsafeRawBufferPointer) throws -> R) throws -> R`](/documentation/Testing/Attachable/withUnsafeBytes(for:_:))

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

## Relationships

### Inherited By

[`AttachableWrapper`](/documentation/Testing/AttachableWrapper)

---

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)