---
title: "contents(forType:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uidocument/contents(fortype:)"
---

# contents(forType:)

Returns the document data to be saved.

## Declaration

```swift
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 doc://com.apple.uikit/documentation/UIKit/UIDocument/fileType property. If you want to save the document under a different UTI, you can override the doc://com.apple.uikit/documentation/UIKit/UIDocument/savingFileType method.

## Return Value

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

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 don’t get called: save(to:for:completionHandler:) autosave(completionHandler:) close(completionHandler:) 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. 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

### Related Documentation

- [open(completionHandler:)](uikit/uidocument/open(completionhandler:).md)
- [revert(toContentsOf:completionHandler:)](uikit/uidocument/revert(tocontentsof:completionhandler:).md)

### Writing document data

- [close(completionHandler:)](uikit/uidocument/close(completionhandler:).md)
- [save(to:for:completionHandler:)](uikit/uidocument/save(to:for:completionhandler:).md)
- [writeContents(_:andAttributes:safelyTo:for:)](uikit/uidocument/writecontents(_:andattributes:safelyto:for:).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)
