---
title: "data(ofType:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsdocument/data(oftype:)"
---

# data(ofType:)

Creates and returns a data object that contains the contents of the document, formatted to a specified type.

## Declaration

```swift
func data(ofType typeName: String) throws -> Data
```

## Parameters

- `typeName`: The string that identifies the document type.

## Return Value

Return Value A data object containing the document contents, or, if the data object could not be created, nil.

## Discussion

Discussion The default implementation of this method throws an exception because at least one of the writing methods (this method, write(to:ofType:), fileWrapper(ofType:), or write(to:ofType:for:originalContentsURL:)) must be overridden. For backward binary compatibility with OS X v10.3 and earlier, the default implementation of this method instead invokes dataRepresentationOfType:``typeName on self if dataRepresentationOfType: is overridden. 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 the Document’s Content

- [canAsynchronouslyWrite(to:ofType:for:)](appkit/nsdocument/canasynchronouslywrite(to:oftype:for:).md)
- [unblockUserInteraction()](appkit/nsdocument/unblockuserinteraction().md)
- [write(to:ofType:)](appkit/nsdocument/write(to:oftype:).md)
- [writeSafely(to:ofType:for:)](appkit/nsdocument/writesafely(to:oftype:for:).md)
- [fileWrapper(ofType:)](appkit/nsdocument/filewrapper(oftype:).md)
- [write(to:ofType:for:originalContentsURL:)](appkit/nsdocument/write(to:oftype:for:originalcontentsurl:).md)
- [save(to:ofType:for:delegate:didSave:contextInfo:)](appkit/nsdocument/save(to:oftype:for:delegate:didsave:contextinfo:).md)
- [save(to:ofType:for:completionHandler:)](appkit/nsdocument/save(to:oftype:for:completionhandler:).md)
- [fileAttributesToWrite(to:ofType:for:originalContentsURL:)](appkit/nsdocument/fileattributestowrite(to:oftype:for:originalcontentsurl:).md)
- [NSDocument.SaveOperationType](appkit/nsdocument/saveoperationtype.md)
