<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocumentController/makeDocument(withContentsOf:ofType:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocumentController(im)makeDocumentWithContentsOfURL:ofType:error:"
  },
  "title" : "makeDocument(withContentsOf:ofType:)"
}
-->

# makeDocument(withContentsOf:ofType:)

Instantiates a document located by a URL, of a specified type, and returns it if successful.

```
func makeDocument(withContentsOf url: URL, ofType typeName: String) throws -> NSDocument
```

## Parameters

`url`

The location of the new document object.

`typeName`

The type of the document.

## Return Value

The newly created [`NSDocument`](/documentation/AppKit/NSDocument) object, or `nil` if the document could not be created.

## Discussion

The URL is specified by `absoluteURL` and the document type by `typeName`. If not successful, the method returns `nil` after setting `outError` to point to an NSError that encapsulates the reason why the document could not be instantiated. The default implementation of this method calls [`documentClass(forType:)`](/documentation/AppKit/NSDocumentController/documentClass(forType:)) to find out the class of document to instantiate, allocates a document object, and initializes it by sending it an [`init(contentsOf:ofType:)`](/documentation/AppKit/NSDocument/init(contentsOf:ofType:)) message.

> Handling Errors in Swift:
> In Swift, this method returns a nonoptional result and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, 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)