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

# writeSafely(to:ofType:for:)

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

```
nonisolated func writeSafely(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType) 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.

## Discussion

The default implementation of this method invokes [`write(to:ofType:for:originalContentsURL:)`](/documentation/AppKit/NSDocument/write(to:ofType:for:originalContentsURL:)). It also invokes [`fileAttributesToWrite(to:ofType:for:originalContentsURL:)`](/documentation/AppKit/NSDocument/fileAttributesToWrite(to:ofType:for:originalContentsURL:)) and writes the returned attributes, if any, to the file. It may copy some attributes from the old on-disk revision of the document at the same time, if applicable.

This method is responsible for doing document writing in a way that minimizes the danger of leaving the disk to which writing is being done in an inconsistent state in the event of an app crash, system crash, hardware failure, power outage, and so on. If you override this method, be sure to invoke the superclass implementation.

For `NSSaveOperation`, the default implementation of this method uses the value in the [`keepBackupFile`](/documentation/AppKit/NSDocument/keepBackupFile) property to determine whether or not the old on-disk revision of the document, if there was one, should be preserved after being renamed.

For backward binary compatibility with OS X v10.3 and earlier, the default implementation of this method instead invokes [`writeWithBackupToFile:ofType:saveOperation:`](/documentation/AppKit/NSDocument/writeWithBackupToFile:ofType:saveOperation:) if that method is is overridden and the URL uses the `file:` scheme. The save operation 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)