<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/write(to:ofType:for:originalContentsURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)writeToURL:ofType:forSaveOperation:originalContentsURL:error:"
  },
  "title" : "write(to:ofType:for:originalContentsURL:)"
}
-->

# write(to:ofType:for:originalContentsURL:)

Writes the contents of the document to a file or file package located by a URL.

```
nonisolated func write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws
```

## Parameters

`url`

The location to which the document contents are written.

`typeName`

The string that identifies the document type.

`saveOperation`

The type of save operation.

`absoluteOriginalContentsURL`

The location of the previously saved copy of the document (if not `nil`).

## Discussion

The default implementation of this method merely invokes `[self writeToURL:absoluteURL ofType:typeName error:outError]`. You can override this method instead of one of the three simple writing methods ([`write(to:ofType:)`](/documentation/AppKit/NSDocument/write(to:ofType:)),[`fileWrapper(ofType:)`](/documentation/AppKit/NSDocument/fileWrapper(ofType:)), and [`data(ofType:)`](/documentation/AppKit/NSDocument/data(ofType:))) if your document writing machinery needs access to the on-disk representation of the document revision that is about to be overwritten. The value of `absoluteURL` is often not the same as `[self fileURL]`. Other times it is not the same as the URL for the final save destination. Likewise, `absoluteOriginalContentsURL` is often not the same value as `[self fileURL]`. If `absoluteOriginalContentsURL` is `nil`, either the document has never been saved or the user deleted the document file since it was opened.

For backward binary compatibility with OS X v10.3 and earlier, if [`writeToFile:ofType:originalFile:saveOperation:`](/documentation/AppKit/NSDocument/writeToFile:ofType:originalFile:saveOperation:) is overridden and both URLs use the `file:` scheme, the default implementation of this method instead invokes:

```objc
[self writeToFile:[absoluteURL path]
      ofType:typeName
      originalFile:[absoluteOriginalContentsURL path]
      saveOperation:aSaveOperation];
```

The save operation used in this case is never `NSAutosaveOperation`; `NSSaveToOperation` is used instead.

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