Instance Method

contents(forType:)

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

Declaration

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

Parameters

typeName

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.

outError

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.

Discussion

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 UIDocumentStateChanged 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

Related Symbols

func revert(toContentsOf: URL, completionHandler: ((Bool) -> Void)? = nil)

Reverts a document to the most recent document data stored on-disk.