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

# save(to:ofType:for:completionHandler:)

Saves the contents of the document to a file or file package located by a URL, that is formatted to a specified type, for a particular kind of save operation, and invokes the passed-in completion handler.

```
func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, completionHandler: @escaping ((any Error)?) -> Void)
```

## Parameters

`url`

The location to which the document contents are written.

`typeName`

The document type.

`saveOperation`

The type of save operation.

`completionHandler`

The completion handler block object passed in to be invoked at some point in the future, perhaps after the method invocation has returned. The completion handler must be invoked on the main thread.

    The block takes one argument:

- `errorOrNil`: If successful, pass a `nil` error. If not successful, pass an `NSError` object that encapsulates the reason why the document could not be saved.

## Discussion

The default implementation of this method invokes [`canAsynchronouslyWrite(to:ofType:for:)`](/documentation/AppKit/NSDocument/canAsynchronouslyWrite(to:ofType:for:)). If writing can’t be done concurrently, it invokes [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) on the main thread thread. If writing can be done concurrently, it invokes that method on a different thread, but blocks the main thread until something invokes [`unblockUserInteraction()`](/documentation/AppKit/NSDocument/unblockUserInteraction()). Either way, if [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) returns <doc://com.apple.documentation/documentation/Swift/true>, it updates the values in the [`fileModificationDate`](/documentation/AppKit/NSDocument/fileModificationDate), [`fileType`](/documentation/AppKit/NSDocument/fileType), [`fileURL`](/documentation/AppKit/NSDocument/fileURL), and [`autosavedContentsFileURL`](/documentation/AppKit/NSDocument/autosavedContentsFileURL) properties and calls the [`updateChangeCount(_:)`](/documentation/AppKit/NSDocument/updateChangeCount(_:)) method as appropriate on the main thread. It also updates information that [`save(withDelegate:didSave:contextInfo:)`](/documentation/AppKit/NSDocument/save(withDelegate:didSave:contextInfo:)) uses to check for modification, renaming, moving, deleting, and trashing of open documents, and deletes autosaved contents files when they have become obsolete. You can override this method to do things that need to be done before or after any save operation but be sure to invoke `super`.

For backward binary compatibility with OS X v10.6 and earlier, the default implementation of this method instead invokes [`saveToURL:ofType:forSaveOperation:error:`](/documentation/AppKit/NSDocument/saveToURL:ofType:forSaveOperation:error:) if that method is overridden and this one is not, and it passes any error to the completion handler.

---

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)