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

# init(for:withContentsOf:ofType:)

Initializes a document with the specified contents, and places the resulting document’s file at the designated location.

```
convenience init(for urlOrNil: URL?, withContentsOf contentsURL: URL, ofType typeName: String) throws
```

## Parameters

`urlOrNil`

The location for the document’s file. This value is `nil` for an autosaved document that the user never explicitly saved.

`contentsURL`

The URL of the file that contains the document’s contents. When loading an autosaved document, this URL contains the location of the autosave file. The contents of this file replace the contents of the file in `absoluteDocumentURL`.

`typeName`

The string that identifies the document type.

## Return Value

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

## Discussion

The system calls this method to open a document that has an associated autosave file . You can override this method to handle any document initialization specific to autosave contents.

After reading the contents from the specified autosave file, this method updates the document’s change count using the `NSChangeReadOtherContents` change type.

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