Instance Method


Autosaves the document’s contents at an appropriate location if it needs autosaving.


func autosave(withImplicitCancellability autosavingIsImplicitlyCancellable: Bool, completionHandler: @escaping (Error?) -> Void)



The value in the autosavingIsImplicitlyCancellable property while autosaving is happening.


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:


If successful, pass a nil error. If not successful, pass an NSError object that encapsulates the reason why the document could not be autosaved.


The default implementation of this method does the following:

  1. Checks the value of the hasUnautosavedChanges property.

  2. If the value of that property is false, the method runs the completion handler with a nil error and returns immediately.

    If the value is true, calls autosavesInPlace on the class to determine where the autosaved document contents should go.

    The method also gets the value in fileURL to ensure that the file has an actual URL, because it is not possible to autosave in place if the document does not yet have a permanent location.

  3. Checks the value in the autosavingFileType property to determine the file type for the autosaved file.

  4. Calls save(to:ofType:for:completionHandler:).

    The value of the saveToURL parameter is the location where the file should be saved. If the file has a URL and the class specifies that autosave should occur in place, this is the URL of the file. Otherwise, this is the location of a nonexistent file in the specified autosave location.

    The value for the ofType parameter is determined by a call to autosavingFileType.

    The value of the forSaveOperation parameter is NSAutosaveInPlaceOperation if the class is configured to autosave in place and the file has a URL. Otherwise, the value is NSAutosaveElsewhereOperation.

See Also


func checkAutosavingSafety()

Returns a Boolean value indicating whether it is probably safe to autosave document changes.

func scheduleAutosaving()

Schedules periodic autosaving for the purpose of crash protection.

var hasUnautosavedChanges: Bool

A Boolean value indicating whether the document has changes that have not been autosaved.

class var autosavesInPlace: Bool

Returns whether the receiver supports autosaving in place.

class var autosavesDrafts: Bool

Returns whether the receiving subclass of NSDocument supports autosaving of drafts.

class var preservesVersions: Bool

Returns whether the receiving subclass of NSDocument supports Versions.

func browseVersions(Any?)

Opens the Versions browser in the document’s main window.

var autosavingFileType: String?

Returns the document type that should be used for an autosave operation.

var autosavingIsImplicitlyCancellable: Bool

A Boolean value indicating whether autosaving is happening now but could be safely cancelled.

var autosavedContentsFileURL: URL?

The location of the most recently autosaved document contents.