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

# initWithContentsOfURL:ofType:

Initializes and returns a document object of a given type.

```
- (id) initWithContentsOfURL:(NSURL *) url ofType:(NSString *) typeName;
```

## Discussion

Initializes and returns an `NSDocument` object of document type `docType` containing data stored at `aURL`. In opening the location, invokes the [`readFromURL:ofType:`](/documentation/AppKit/NSDocument/readFromURL:ofType:) method. If the document successfully opens the location, it updates [`fileURL`](/documentation/AppKit/NSDocument/fileURL) and [`fileType`](/documentation/AppKit/NSDocument/fileType). If the location cannot be opened, or the document is unable to load the contents of the location, this method returns `nil`. This initializer is typically invoked by the `NSDocumentController` method [`makeDocumentWithContentsOfURL:ofType:`](/documentation/AppKit/NSDocumentController/makeDocumentWithContentsOfURL: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)