---
title: "init(contentsOf:ofType:)"
framework: appkit
role: symbol
role_heading: Initializer
path: "appkit/nsdocument/init(contentsof:oftype:)"
---

# init(contentsOf:ofType:)

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

## Declaration

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

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

## Discussion

Discussion You can override this method to customize the reopening of autosaved documents. This method is invoked by the NSDocumentController method makeDocument(withContentsOf:ofType:). The default implementation of this method calls the init() and read(from:ofType:) methods and sets values for the fileURL, fileType, and fileModificationDate properties. For backward binary compatibility with OS X v10.3 and earlier, the default implementation of this method instead invokes initWithContentsOfFile:ofType: if it is overridden and the URL uses the file: scheme. It still updates the  fileModificationDate property in this situation. note: 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 in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Creating a Document Object

- [init()](appkit/nsdocument/init().md)
- [init(for:withContentsOf:ofType:)](appkit/nsdocument/init(for:withcontentsof:oftype:).md)
- [init(type:)](appkit/nsdocument/init(type:).md)
