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

# writeWithBackupToFile:ofType:saveOperation:

This method is called by action methods to save document contents to a file.

```
- (BOOL) writeWithBackupToFile:(NSString *) fullDocumentPath ofType:(NSString *) documentTypeName saveOperation:(NSSaveOperationType) saveOperationType;
```

## Discussion

This method is called by action methods like [`save(_:)`](/documentation/AppKit/NSDocument/save(_:)), [`saveAs(_:)`](/documentation/AppKit/NSDocument/saveAs(_:)), and [`saveTo(_:)`](/documentation/AppKit/NSDocument/saveTo(_:)). It is responsible for handling backup of the existing file, if any, and removal of that backup if [`keepBackupFile`](/documentation/AppKit/NSDocument/keepBackupFile) returns <doc://com.apple.documentation/documentation/Swift/false>. In between those two things, it calls [`writeToFile:ofType:originalFile:saveOperation:`](/documentation/AppKit/NSDocument/writeToFile:ofType:originalFile:saveOperation:) to write the document of type `docType` to `fullDocumentPath`. You should never need to call [`writeWithBackupToFile:ofType:saveOperation:`](/documentation/AppKit/NSDocument/writeWithBackupToFile:ofType:saveOperation:), but subclasses that want to change the way the backup works can override it. The `saveOperationType` argument is one of the constants listed in `Constants`.

If you override this method, you should invoke [`fileAttributesToWriteToFile:ofType:saveOperation:`](/documentation/AppKit/NSDocument/fileAttributesToWriteToFile:ofType:saveOperation:) and set the variables returned from this method when writing `fullDocumentPath`. `NSFileManager` <doc://com.apple.documentation/documentation/Foundation/FileManager/setAttributes(_:ofItemAtPath:)> can be used to do this.

---

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)