<!--
{
  "availability" : [
    "macCatalyst: -",
    "macOS: 10.0.0 - 10.4.0"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/initWithContentsOfFile:ofType:",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)initWithContentsOfFile:ofType:"
  },
  "title" : "initWithContentsOfFile:ofType:"
}
-->

# initWithContentsOfFile:ofType:

Initializes and returns a document object.

```
- (id) initWithContentsOfFile:(NSString *) absolutePath ofType:(NSString *) typeName;
```

## Discussion

Initializes and returns an NSDocument object of document type `docType` containing data stored in the file `fileName`. In opening the file, invokes the [`readFromFile:ofType:`](/documentation/AppKit/NSDocument/readFromFile:ofType:) method. If the document successfully opens the file, it updates [`fileURL`](/documentation/AppKit/NSDocument/fileURL) and [`fileType`](/documentation/AppKit/NSDocument/fileType). If the file cannot be opened, or the document is unable to load the contents of the file, this method returns `nil`. This initializer is typically invoked by the `NSDocumentController` method [`makeDocumentWithContentsOfFile:ofType:`](/documentation/AppKit/NSDocumentController/makeDocumentWithContentsOfFile:ofType:).

---

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)