<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/read(from:ofType:)-6g6ai",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)readFromData:ofType:error:"
  },
  "title" : "read(from:ofType:)"
}
-->

# read(from:ofType:)

Sets the contents of this document by reading from data of a specified type.

```
nonisolated func read(from data: Data, ofType typeName: String) throws
```

## Parameters

`data`

The data object from which the document contents are read.

`typeName`

The string that identifies the document type.

## Discussion

The default implementation of this method throws an exception because at least one of the three reading methods (this method, [`read(from:ofType:)`](/documentation/AppKit/NSDocument/read(from:ofType:)-1vttv), [`read(from:ofType:)`](/documentation/AppKit/NSDocument/read(from:ofType:)-3rzsi)), or every method that may invoke [`read(from:ofType:)`](/documentation/AppKit/NSDocument/read(from:ofType:)-1vttv), must be overridden.

> 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`.

---

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)