<!--
{
  "availability" : [
    "Swift: 6.4.0 -",
    "Xcode: 27.0.0 -",
    "iOS: 18.2.0 -",
    "iPadOS: 18.2.0 -",
    "macCatalyst: 18.2.0 -",
    "macOS: 15.2.0 -",
    "swift: 6.0.0 -",
    "tvOS: 18.2.0 -",
    "visionOS: 2.2.0 -",
    "watchOS: 11.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/Attachment/init(exporting:as:named:sourceLocation:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift Testing",
      "CoreTransferable",
      "Testing"
    ],
    "preciseIdentifier" : "s:7Testing10AttachmentV01_A17_CoreTransferableE9exporting2as5named14sourceLocationACyAD011_AttachableD7WrapperVyqd__GGqd___22UniformTypeIdentifiers6UTTypeVSgSSSgAA06SourceI0VtYaKcAKRsz0cD00D0Rd__lufc"
  },
  "title" : "init(exporting:as:named:sourceLocation:)"
}
-->

# init(exporting:as:named:sourceLocation:)

Initialize an instance of this type that encloses the given transferable
value.

```
init<T>(exporting transferableValue: T, as contentType: UTType? = nil, named preferredName: String? = nil, sourceLocation: SourceLocation = #_sourceLocation) async throws where AttachableValue == _AttachableTransferableWrapper<T>, T : Transferable
```

## Parameters

`transferableValue`

The value that will be attached to the output of
the test run.

`contentType`

The content type with which to export `transferableValue`.
If this argument is `nil`, the testing library calls [`exportedContentTypes(_:)`](https://developer.apple.com/documentation/coretransferable/transferable/exportedcontenttypes(_:))
on `transferableValue` and uses the first type the function returns
that conforms to [`UTType.data`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/data).

`preferredName`

The preferred name of the attachment to use when saving
it. If `nil`, the testing library attempts to generate a reasonable
filename for the attached value.

`sourceLocation`

The source location of the call to this initializer.
This value is used when recording issues associated with the
attachment.

## Discussion> Throws: Any error that occurs while exporting `transferableValue`.

Use this initializer to create an instance of [`Attachment`](/documentation/Testing/Attachment) from a value
that conforms to the [`Transferable`](https://developer.apple.com/documentation/coretransferable/transferable)
protocol.

```swift
let menu = FoodTruck.menu
let attachment = try await Attachment(exporting: menu, as: .pdf)
Attachment.record(attachment)
```

When you call this initializer and pass it a transferable value, it
calls [`exported(as:)`](https://developer.apple.com/documentation/coretransferable/transferable/exported(as:))
on that value. This operation may take some time, so this initializer
suspends the calling task until it is complete.

---

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)