<!--
{
  "availability" : [
    "Swift: 6.3.0 -",
    "Xcode: 26.4.0 -",
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "swift: 6.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/Attachment/init(_:named:as:sourceLocation:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift Testing",
      "Testing"
    ],
    "preciseIdentifier" : "s:7Testing10AttachmentV_5named2as14sourceLocationACyxGqd___SSSgAA21AttachableImageFormatVSgAA06SourceF0VtcAA01_gH7WrapperCyqd__GRbzAA0gK0RzAA0g2AsH0Rd__lufc"
  },
  "title" : "init(_:named:as:sourceLocation:)"
}
-->

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

Initialize an instance of this type that encloses the given image.

```
init<T>(_ image: T, named preferredName: String? = nil, as imageFormat: AttachableImageFormat? = nil, sourceLocation: SourceLocation = #_sourceLocation) where AttachableValue : _AttachableImageWrapper<T>, AttachableValue : AttachableWrapper, T : AttachableAsImage
```

## Parameters

`image`

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

`preferredName`

The preferred name of the attachment when writing it
to a test report or to disk. If `nil`, the testing library attempts
to derive a reasonable filename for the attached value.

`imageFormat`

The image format with which to encode `image`.

`sourceLocation`

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

## Discussion

The testing library uses the image format specified by `imageFormat`. Pass
`nil` to let the testing library decide which image format to use. If you
pass `nil`, then the image format that the testing library uses depends on
the path extension you specify in `preferredName`, if any. If you do not
specify a path extension, or if the path extension you specify doesn’t
correspond to an image format the operating system knows how to write, the
testing library selects an appropriate image format for you.

---

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)