Instance Method


Override this method to return the document data to be saved.


func contents(forType typeName: String) throws -> Any



The file type of the document, a Uniform Type Identifier (UTI). This string typically derives from the fileType property. If you want to save the document under a different UTI, you can override the savingFileType method.


If you cannot return document data, return by indirection an NSError object that encapsulates the reasons you can’t. Otherwise, ignore this parameter.

Return Value

The document data to be saved, or nil if you cannot return document data. The returned object is typically an instance of the NSData class for flat files or the FileWrapper class for file packages. If you return nil, you should also return an error object in outError.

If you return an object other than an NSData or FileWrapper object, you must override the writeContents(_:andAttributes:safelyTo:for:) or writeContents(_:to:for:originalContentsURL:) method to handle the writing of data.


When you subclass UIDocument, override this method to provide UIKit with the document data for saving.

This method is called on the queue that the save(to:for:completionHandler:) method was called on (typically, the main queue). Writing of data occurs on a background queue. The default implementation of this method returns nil.

When you return a non-nil value in the outError parameter, the completion handlers for the following methods do not get called:

Instead, in this case, the error is available to your app in the handleError(_:userInteractionPermitted:) method and in the stateChangedNotification notification.

If you want more control over the saving operation than this method provides—for example, if you want to perform incremental writing of data—override, instead, one of the lower-level data-writing methods such as writeContents(_:andAttributes:safelyTo:for:) or writeContents(_:to:for:originalContentsURL:). These methods are called on a background thread.

See Also

Writing Document Data

func close(completionHandler: ((Bool) -> Void)?)

Asynchronously closes the document after saving any changes.

func save(to: URL, for: UIDocument.SaveOperation, completionHandler: ((Bool) -> Void)?)

Saves document data to the specified location in the application sandbox.

func writeContents(Any, andAttributes: [AnyHashable : Any]?, safelyTo: URL, for: UIDocument.SaveOperation)

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

func writeContents(Any, to: URL, for: UIDocument.SaveOperation, originalContentsURL: URL?)

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

var savingFileType: String?

Returns the file type to use for saving a document.

func fileAttributesToWrite(to: URL, for: UIDocument.SaveOperation) -> [AnyHashable : Any]

Returns a dictionary of file attributes to associate with the document file when writing or updating it.

func fileNameExtension(forType: String?, saveOperation: UIDocument.SaveOperation) -> String

Returns a file extension to append to the file URL of the document file being written.