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

# makeDocument(for:withContentsOf:ofType:)

Instantiates a document located by a URL, of a specified type, but by reading the contents for the document from another URL, and returns it if successful.

```
func makeDocument(for urlOrNil: URL?, withContentsOf contentsURL: URL, ofType typeName: String) throws -> NSDocument
```

## Parameters

`urlOrNil`

The location of the new document object.

`contentsURL`

The URL of the file that provides the contents for the new document.

`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 `absoluteDocumentURL`, the type by `typeName`, and the other URL providing the contents by `absoluteDocumentContentsURL`. If not successful, the method returns `nil` after setting `outError` to point to an `NSError` object 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(for:withContentsOf:ofType:)`](/documentation/AppKit/NSDocument/init(for:withContentsOf: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)