---
title: "writeContents(_:andAttributes:safelyTo:for:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uidocument/writecontents(_:andattributes:safelyto:for:)"
---

# writeContents(_:andAttributes:safelyTo:for:)

Ensures that document data is written safely to a specified location in the application sandbox.

## Declaration

```swift
func writeContents(_ contents: Any, andAttributes additionalFileAttributes: [AnyHashable : Any]? = nil, safelyTo url: URL, for saveOperation: UIDocument.SaveOperation) 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(_:to:for:originalContentsURL:) to perform the actual writing of the data.
- `additionalFileAttributes`: A dictionary of doc://com.apple.documentation/documentation/Foundation/FileManager file attributes to assign to the document file. The default implementation obtains these file attributes by calling doc://com.apple.uikit/documentation/UIKit/UIDocument/fileAttributesToWrite(to:for:).
- `url`: The 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.

## Discussion

Discussion This method is called by the save(to:for:completionHandler:) method to save the file data (and associated attributes in the case of an FileWrapper). It creates temporary files and directories as necessary so that successful saves can be completed atomically and failed saves can be rolled back cleanly. This method calls writeContents(_:to:for:originalContentsURL:) to save the contents object, passing the location for the new saved file in the toURL parameter and the location of the previously existing file in the originalContentsURL parameter, if this is an overwrite operation. If you want to change how file data is saved, you generally override the writeContents(_:to:for:originalContentsURL:) method instead of this method. Additionally, you don’t need to call this method directly unless you are overriding the save(to:for:completionHandler:) method. 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(_:to:for:originalContentsURL:)](uikit/uidocument/writecontents(_:to:for:originalcontentsurl:).md)
- [savingFileType](uikit/uidocument/savingfiletype.md)
- [fileAttributesToWrite(to:for:)](uikit/uidocument/fileattributestowrite(to:for:).md)
- [fileNameExtension(forType:saveOperation:)](uikit/uidocument/filenameextension(fortype:saveoperation:).md)
