<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIDocument/contents(forType:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIDocument(im)contentsForType:error:"
  },
  "title" : "contents(forType:)"
}
-->

# contents(forType:)

Returns the document data to be saved.

```
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`](/documentation/UIKit/UIDocument/fileType) property. If you want to save the document under a different UTI, you can override the [`savingFileType`](/documentation/UIKit/UIDocument/savingFileType) method.

## 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 <doc://com.apple.documentation/documentation/Foundation/NSData> class for flat files or the <doc://com.apple.documentation/documentation/Foundation/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   <doc://com.apple.documentation/documentation/Foundation/NSData>   or   <doc://com.apple.documentation/documentation/Foundation/FileWrapper>   object, you must override the [`writeContents(_:andAttributes:safelyTo:for:)`](/documentation/UIKit/UIDocument/writeContents(_:andAttributes:safelyTo:for:))   or [`writeContents(_:to:for:originalContentsURL:)`](/documentation/UIKit/UIDocument/writeContents(_:to:for:originalContentsURL:))   method to handle the writing of data.

## Discussion

When you subclass [`UIDocument`](/documentation/UIKit/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:)`](/documentation/UIKit/UIDocument/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:)`](/documentation/UIKit/UIDocument/save(to:for:completionHandler:))
- [`autosave(completionHandler:)`](/documentation/UIKit/UIDocument/autosave(completionHandler:))
- [`close(completionHandler:)`](/documentation/UIKit/UIDocument/close(completionHandler:))

Instead, in this case, the error is available to your app in the [`handleError(_:userInteractionPermitted:)`](/documentation/UIKit/UIDocument/handleError(_:userInteractionPermitted:)) method and in the [`stateChangedNotification`](/documentation/UIKit/UIDocument/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:)`](/documentation/UIKit/UIDocument/writeContents(_:andAttributes:safelyTo:for:)) or [`writeContents(_:to:for:originalContentsURL:)`](/documentation/UIKit/UIDocument/writeContents(_:to:for:originalContentsURL:)). These methods are called on a background thread.

> Handling Errors in Swift:
> 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](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

## See Also

[`open(completionHandler:)`](/documentation/UIKit/UIDocument/open(completionHandler:))

Opens a document asynchronously.

[`revert(toContentsOf:completionHandler:)`](/documentation/UIKit/UIDocument/revert(toContentsOf:completionHandler:))

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)