---
title: "makeDocument(withContentsOf:ofType:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsdocumentcontroller/makedocument(withcontentsof:oftype:)"
---

# makeDocument(withContentsOf:ofType:)

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

## Declaration

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

Return Value The newly created NSDocument object, or nil if the document could not be created.

## Discussion

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:) to find out the class of document to instantiate, allocates a document object, and initializes it by sending it an init(contentsOf:ofType:) message. note: 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 in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Creating and Opening Documents

- [document(for:)](appkit/nsdocumentcontroller/document(for:)-i5zi.md)
- [duplicateDocument(withContentsOf:copying:displayName:)](appkit/nsdocumentcontroller/duplicatedocument(withcontentsof:copying:displayname:).md)
- [openDocument(withContentsOf:display:completionHandler:)](appkit/nsdocumentcontroller/opendocument(withcontentsof:display:completionhandler:).md)
- [openUntitledDocumentAndDisplay(_:)](appkit/nsdocumentcontroller/openuntitleddocumentanddisplay(_:).md)
- [makeDocument(for:withContentsOf:ofType:)](appkit/nsdocumentcontroller/makedocument(for:withcontentsof:oftype:).md)
- [makeUntitledDocument(ofType:)](appkit/nsdocumentcontroller/makeuntitleddocument(oftype:).md)
- [reopenDocument(for:withContentsOf:display:completionHandler:)](appkit/nsdocumentcontroller/reopendocument(for:withcontentsof:display:completionhandler:).md)
