<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/init(contentsOf:ofType:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)initWithContentsOfURL:ofType:error:"
  },
  "title" : "init(contentsOf:ofType:)"
}
-->

# init(contentsOf:ofType:)

Initializes a document located by a URL of a specified type.

```
convenience init(contentsOf url: URL, ofType typeName: String) throws
```

## Parameters

`url`

The URL from which the contents of the document are obtained.

`typeName`

The string that identifies the document type.

## Return Value

The initialized `NSDocument` object, or, if the document could not be created, `nil`.

## Discussion

You can override this method to customize the reopening of autosaved documents.

This method is invoked by the `NSDocumentController` method [`makeDocument(withContentsOf:ofType:)`](/documentation/AppKit/NSDocumentController/makeDocument(withContentsOf:ofType:)). The default implementation of this method calls the [`init()`](/documentation/AppKit/NSDocument/init()) and [`read(from:ofType:)`](/documentation/AppKit/NSDocument/read(from:ofType:)-1vttv) methods and sets values for the [`fileURL`](/documentation/AppKit/NSDocument/fileURL), [`fileType`](/documentation/AppKit/NSDocument/fileType), and [`fileModificationDate`](/documentation/AppKit/NSDocument/fileModificationDate) properties.

For backward binary compatibility with OS X v10.3 and earlier, the default implementation of this method instead invokes [`initWithContentsOfFile:ofType:`](/documentation/AppKit/NSDocument/initWithContentsOfFile:ofType:) if it is overridden and the URL uses the `file:` scheme. It still updates the  [`fileModificationDate`](/documentation/AppKit/NSDocument/fileModificationDate) property in this situation.

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