<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIDocument/updateChangeCount(withToken:for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIDocument(im)updateChangeCountWithToken:forSaveOperation:"
  },
  "title" : "updateChangeCount(withToken:for:)"
}
-->

# updateChangeCount(withToken:for:)

Updates the change count with reference to a change-count token passed in by UIKit.

```
func updateChangeCount(withToken changeCountToken: Any, for saveOperation: UIDocument.SaveOperation)
```

## Parameters

`changeCountToken`

An object to use as a change-count token. `UIDocument` obtained this token earlier by calling [`changeCountToken(for:)`](/documentation/UIKit/UIDocument/changeCountToken(for:)).

`saveOperation`

A constant that indicates whether the save operation is writing a new file or overwriting an existing one. See [`UIDocument.SaveOperation`](/documentation/UIKit/UIDocument/SaveOperation) for descriptions of these constants.

## Discussion

To get autosaving capabilities for your documents, you must implement change tracking. Typically you do this by using an <doc://com.apple.documentation/documentation/Foundation/UndoManager> object (assigned to [`undoManager`](/documentation/UIKit/UIDocument/undoManager) property) to register changes or by calling the [`updateChangeCount(_:)`](/documentation/UIKit/UIDocument/updateChangeCount(_:)) method every time the user makes a change; UIKit then automatically determines whether there are unsaved changes and returns the proper value from [`hasUnsavedChanges`](/documentation/UIKit/UIDocument/hasUnsavedChanges). If you take neither of these approaches (and you want the autosaving feature), you must implement this method as well as [`changeCountToken(for:)`](/documentation/UIKit/UIDocument/changeCountToken(for:)) and [`hasUnsavedChanges`](/documentation/UIKit/UIDocument/hasUnsavedChanges).

You implement the [`changeCountToken(for:)`](/documentation/UIKit/UIDocument/changeCountToken(for:)) method to return a change-count token that UIKit uses to encapsulate the history of document changes for a particular save operation. The token can be any object that represents the current change state of the document. This method is called at the start of the default implementation of the [`save(to:for:completionHandler:)`](/documentation/UIKit/UIDocument/save(to:for:completionHandler:)) method. At the end of this default implementation, [`UIDocument`](/documentation/UIKit/UIDocument) calls this method ([`updateChangeCount(withToken:for:)`](/documentation/UIKit/UIDocument/updateChangeCount(withToken:for:))), passing in the change-count token. You implement this method to compare the token with the one returned earlier; by doing so, you can determine if the document has acquired new unsaved changes between the start and end of an asynchronous write operation. You can then return the proper value from your override of [`hasUnsavedChanges`](/documentation/UIKit/UIDocument/hasUnsavedChanges).

---

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)