---
title: "writeContents(_:to:for:originalContentsURL:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uidocument/writecontents(_:to:for:originalcontentsurl:)"
---

# writeContents(_:to:for:originalContentsURL:)

Writes the document data to disk at the sandbox location indicated by a file URL.

## Declaration

```swift
func writeContents(_ contents: Any, to url: URL, for saveOperation: UIDocument.SaveOperation, originalContentsURL: URL?) throws
```

## Parameters

- `contents`: The document data to write to disk. Typically, the data is encapsulated by an doc://com.apple.documentation/documentation/Foundation/NSData object (if a flat file) or an doc://com.apple.documentation/documentation/Foundation/FileWrapper object (if a file package). If the object encapsulating the document data is of some other type, you should override this method or doc://com.apple.uikit/documentation/UIKit/UIDocument/writeContents(_:andAttributes:safelyTo:for:) to perform the actual writing of the data.
- `url`: A file URL specifying the location of the document file in the application sandbox.
- `saveOperation`: A constant that indicates whether the document file is being written the first time or whether it is being overwritten. See doc://com.apple.uikit/documentation/UIKit/UIDocument/SaveOperation for details.
- `originalContentsURL`: A file URL specifying the previous location of the document file (if not nil).

## Discussion

Discussion This method is called by the writeContents(_:andAttributes:safelyTo:for:) to write the actual file data. It is passed the contents object returned from your contents(forType:) implementation. The default implementation of this method supports NSData or FileWrapper contents by asking the contents object to save itself to the corresponding URL. If you override this method, you may choose to return a different type of data from contents(forType:) or you may choose to not override contents(forType:) and generate the writable data directly within this method. If you override this method, you should not invoke the superclass implementation. note: In Swift, this method is marked with the throws keyword to indicate that it throws an error in cases of failure. When overriding this method, use the throw statement to throw an NSError, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Writing document data

- [close(completionHandler:)](uikit/uidocument/close(completionhandler:).md)
- [contents(forType:)](uikit/uidocument/contents(fortype:).md)
- [save(to:for:completionHandler:)](uikit/uidocument/save(to:for:completionhandler:).md)
- [writeContents(_:andAttributes:safelyTo:for:)](uikit/uidocument/writecontents(_:andattributes:safelyto:for:).md)
- [savingFileType](uikit/uidocument/savingfiletype.md)
- [fileAttributesToWrite(to:for:)](uikit/uidocument/fileattributestowrite(to:for:).md)
- [fileNameExtension(forType:saveOperation:)](uikit/uidocument/filenameextension(fortype:saveoperation:).md)
