init(contentsOf:named:sourceLocation:)
Initialize an instance of this type with the contents of the given URL.
Declaration
init(contentsOf url: URL, named preferredName: String? = nil, sourceLocation: SourceLocation = #_sourceLocation) async throwsParameters
- url:
The URL containing the attachment’s data.
- preferredName:
The preferred name of the attachment when writing it to a test report or to disk. If
nil, the name of the attachment is derived from the last path component ofurl. - sourceLocation:
The source location of the call to this initializer. This value is used when recording issues associated with the attachment.
Discussion
Use this initializer to create an instance of Attachment that represents a local file or directory:
let url = try await FoodTruck.saveMenu(as: .pdf)
let attachment = try await Attachment(contentsOf: url)
Attachment.record(attachment)When you call this initializer and pass it the URL of a file, it reads or maps the contents of that file into memory. When you call this initializer and pass it the URL of a directory, it creates a temporary ZIP file of the directory before reading or mapping it into memory. These operations may take some time, so this initializer suspends the calling task until they are complete.