<!--
{
  "availability" : [
    "Swift: -"
  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTAttachment/init(contentsOfFileAtURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "c:objc(cs)XCTAttachment(cm)attachmentWithContentsOfFileAtURL:::SYNTHESIZED::c:objc(cs)XCTAttachment"
  },
  "title" : "init(contentsOfFileAtURL:)"
}
-->

# init(contentsOfFileAtURL:)

Creates an attachment from the contents of an existing file on disk.

```
convenience init(contentsOfFileAtURL url: URL)
```

## Parameters

`url`

The file URL from which data should be read to create the new attachment.

## Discussion

The attachment’s [`name`](/documentation/XCTest/XCTAttachment/name) property is the file name of the provided url. The attachment’s [`uniformTypeIdentifier`](/documentation/XCTest/XCTAttachment/uniformTypeIdentifier) value is inferred from the file’s extension. A default UTI of `"public.data"` is used for files without an extension.

> Note:
> Use this initializer with files only. To create an attachment from a directory, use ``doc://com.apple.xctest/documentation/XCTest/XCTAttachment/init(compressedContentsOfDirectoryAtURL:)``.

---

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)