<!--
{
  "availability" : [
    "iOS: 3.0.0 -",
    "iPadOS: 3.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/UndoManager",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUndoManager"
  },
  "title" : "UndoManager"
}
-->

# UndoManager

A general-purpose recorder of operations that enables undo and redo.

```
@MainActor class UndoManager
```

## Overview

You register an undo operation by calling one of the methods described in Registering undo operations. You specify the name of the object that’s changing (or the owner of that object) and provide a closure, method, or invocation to revert its state.

After you register an undo operation, you can call [`undo()`](/documentation/Foundation/UndoManager/undo()) on the undo manager to revert to the state of the last undo operation. When undoing an action, [`UndoManager`](/documentation/Foundation/UndoManager) saves the operations you revert to so that you can call [`redo()`](/documentation/Foundation/UndoManager/redo()) automatically.

Typically, apps with UI interactions work with [`UndoManager`](/documentation/Foundation/UndoManager). For example, UIKit implements undo and redo in its text view object, making it easy for you to undo and redo actions in objects along the responder chain. [`UndoManager`](/documentation/Foundation/UndoManager) also serves as a general-purpose state manager, which you can use to undo and redo many kinds of actions. For example, an interactive command-line utility can use this class to undo the last command run, or a networking library can undo a request by sending another request that invalidates the previous one.

> Important: `UndoManager` is <doc://com.apple.documentation/documentation/Swift/MainActor>-isolated in Swift, making it safe to use in UI frameworks like <doc://com.apple.documentation/documentation/AppKit> and <doc://com.apple.documentation/documentation/UIKit> that expect to execute code on the main thread, queue, or actor. When registering an undoable action with ``doc://com.apple.foundation/documentation/Foundation/UndoManager/registerUndo(withTarget:handler:)``, the `handler` closure is also <doc://com.apple.documentation/documentation/Swift/MainActor>-isolated to ensure safety and simplify ergonomics.

## Topics

### Registering undo operations

[`registerUndo(withTarget:handler:)`](/documentation/Foundation/UndoManager/registerUndo(withTarget:handler:))

Registers the specified closure to implement a single undo operation that the target receives.

[`-  registerUndoWithTarget:handler:`](/documentation/Foundation/NSUndoManager/registerUndoWithTarget:handler:)

Records a single undo operation for a given target so that when the manager performs an undo, it executes the specified block.

[`-  registerUndoWithTarget:selector:object:`](/documentation/Foundation/UndoManager/registerUndo(withTarget:selector:object:))

Registers the selector of the specified target to implement a single undo operation that the target receives.

[`-  prepareWithInvocationTarget:`](/documentation/Foundation/UndoManager/prepare(withInvocationTarget:))

Prepares the undo manager for invocation-based undo with the given target as the subject of the next undo operation.

### Checking undo ability

[`canUndo`](/documentation/Foundation/UndoManager/canUndo)

A Boolean value that indicates whether the manager has any actions to undo.

[`canRedo`](/documentation/Foundation/UndoManager/canRedo)

A Boolean value that indicates whether the manager has any actions to redo.

### Performing undo and redo

[`-  undo`](/documentation/Foundation/UndoManager/undo())

Closes the top-level undo group if necessary, and then performs undo operations on the group.

[`-  undoNestedGroup`](/documentation/Foundation/UndoManager/undoNestedGroup())

Performs the undo operations in the last undo group (whether top-level or nested), recording the operations on the redo stack as a single group.

[`-  redo`](/documentation/Foundation/UndoManager/redo())

Performs the operations in the last group on the redo stack, if there are any, recording them on the undo stack as a single group.

### Managing undo and redo stack depth

[`levelsOfUndo`](/documentation/Foundation/UndoManager/levelsOfUndo)

The maximum number of top-level undo groups the undo manager holds.

[`undoCount`](/documentation/Foundation/UndoManager/undoCount)

The number of times you can invoke undo before there are no actions left to undo.

[`redoCount`](/documentation/Foundation/UndoManager/redoCount)

The number of times you can invoke redo before there are no actions left to redo.

### Creating undo groups

[`-  beginUndoGrouping`](/documentation/Foundation/UndoManager/beginUndoGrouping())

Marks the beginning of an undo group.

[`-  endUndoGrouping`](/documentation/Foundation/UndoManager/endUndoGrouping())

Marks the end of an undo group.

[`groupsByEvent`](/documentation/Foundation/UndoManager/groupsByEvent)

A Boolean value that indicates whether the manager automatically creates undo groups around each pass of the run loop.

[`groupingLevel`](/documentation/Foundation/UndoManager/groupingLevel)

The number of nested undo groups (or redo groups, if redo is the most recent operation) in the current event loop.

### Enabling and disabling undo

[`-  disableUndoRegistration`](/documentation/Foundation/UndoManager/disableUndoRegistration())

Disables the recording of undo operations.

[`-  enableUndoRegistration`](/documentation/Foundation/UndoManager/enableUndoRegistration())

Enables the recording of undo operations.

[`undoRegistrationEnabled`](/documentation/Foundation/UndoManager/isUndoRegistrationEnabled)

A Boolean value that indicates whether the recording of undo operations is enabled.

### Checking whether undo or redo is in process

[`undoing`](/documentation/Foundation/UndoManager/isUndoing)

Returns a Boolean value that indicates whether the manager is in the process of performing an undo action.

[`redoing`](/documentation/Foundation/UndoManager/isRedoing)

Returns a Boolean value that indicates whether the manager is in the process of performing a redo action.

### Clearing undo operations

[`-  removeAllActions`](/documentation/Foundation/UndoManager/removeAllActions())

Clears the undo and redo stacks and reenables the manager.

[`-  removeAllActionsWithTarget:`](/documentation/Foundation/UndoManager/removeAllActions(withTarget:))

Clears the undo and redo stacks of all operations involving the specified target as the recipient of the undo message.

### Managing the action name

[`undoActionName`](/documentation/Foundation/UndoManager/undoActionName)

The name identifying the undo action.

[`redoActionName`](/documentation/Foundation/UndoManager/redoActionName)

The name identifying the redo action.

[`setActionName(_:)`](/documentation/Foundation/UndoManager/setActionName(_:)-cci9)

Sets the name of the action associated with the Undo or Redo command using a localized string resource.

[`-  setActionName:`](/documentation/Foundation/UndoManager/setActionName(_:)-8lzip)

Sets the name of the action associated with the Undo or Redo command.

### Getting and localizing the menu item title

[`undoMenuItemTitle`](/documentation/Foundation/UndoManager/undoMenuItemTitle)

The title of the Undo menu command, such as Undo Paste.

[`redoMenuItemTitle`](/documentation/Foundation/UndoManager/redoMenuItemTitle)

The title of the Redo menu command, such as Redo Paste.

[`-  undoMenuTitleForUndoActionName:`](/documentation/Foundation/UndoManager/undoMenuTitle(forUndoActionName:))

Returns the localized title of the Undo menu command for the identified action.

[`-  redoMenuTitleForUndoActionName:`](/documentation/Foundation/UndoManager/redoMenuTitle(forUndoActionName:))

Returns the localized title of the Redo menu command for the identified action.

### Working with user info

[`-  setActionUserInfoValue:forKey:`](/documentation/Foundation/UndoManager/setActionUserInfoValue(_:forKey:))

Sets a user info value for an undo or redo action.

[`-  undoActionUserInfoValueForKey:`](/documentation/Foundation/UndoManager/undoActionUserInfoValue(forKey:))

Retrieves the undo action’s user info value for the given key.

[`-  redoActionUserInfoValueForKey:`](/documentation/Foundation/UndoManager/redoActionUserInfoValue(forKey:))

Retrieves the redo action’s user info value for the given key.

[`UserInfoKey`](/documentation/Foundation/UndoManager/UserInfoKey)

An extensible namespace for undo and redo user info keys.

### Working with run loops

[`runLoopModes`](/documentation/Foundation/UndoManager/runLoopModes)

The modes governing the types of input to handle during a cycle of the run loop.

[`NSUndoCloseGroupingRunLoopOrdering`](/documentation/Foundation/NSUndoCloseGroupingRunLoopOrdering)

A priority to use when using a run loop to close an undo group.

### Using discardable undo and redo actions

[`-  setActionIsDiscardable:`](/documentation/Foundation/UndoManager/setActionIsDiscardable(_:))

Sets whether the next undo or redo action is discardable.

[`undoActionIsDiscardable`](/documentation/Foundation/UndoManager/undoActionIsDiscardable)

A Boolean value that indicates whether the next undo action is discardable.

[`redoActionIsDiscardable`](/documentation/Foundation/UndoManager/redoActionIsDiscardable)

A Boolean value that indicates whether the next redo action is discardable.

### Working with notifications

[`NSUndoManagerWillUndoChangeNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerWillUndoChange)

Posted just before an undo manager performs an undo operation.

[`NSUndoManagerDidUndoChangeNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidUndoChange)

Posted just after an undo manager performs an undo operation.

[`NSUndoManagerWillRedoChangeNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerWillRedoChange)

Posted just before an undo manager performs a redo operation.

[`NSUndoManagerDidRedoChangeNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidRedoChange)

Posted just after an undo manager performs a redo operation.

[`NSUndoManagerCheckpointNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerCheckpoint)

Posted whenever an undo manager opens or closes an undo group (except when it opens a top-level group) and when checking the redo stack.

[`NSUndoManagerDidOpenUndoGroupNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidOpenUndoGroup)

Posted whenever an undo manager opens an undo group.

[`NSUndoManagerWillCloseUndoGroupNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerWillCloseUndoGroup)

Posted before an undo manager closes an undo group.

[`NSUndoManagerDidCloseUndoGroupNotification`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidCloseUndoGroup)

Posted after an undo manager closes an undo group.

[`NSUndoManagerGroupIsDiscardableKey`](/documentation/Foundation/NSUndoManagerGroupIsDiscardableKey)

A key, used in a notification’s user info, that indicates the undo group contains only discardable actions.

### Working with notification messages

[`WillUndoChangeMessage`](/documentation/Foundation/UndoManager/WillUndoChangeMessage)

A message that an undo manager sends before undoing a change.

[`DidUndoChangeMessage`](/documentation/Foundation/UndoManager/DidUndoChangeMessage)

A message that an undo manager sends after undoing a change.

[`WillRedoChangeMessage`](/documentation/Foundation/UndoManager/WillRedoChangeMessage)

A message that an undo manager sends before redoing a change.

[`DidRedoChangeMessage`](/documentation/Foundation/UndoManager/DidRedoChangeMessage)

A message that an undo manager sends after redoing a change.

[`CheckpointMessage`](/documentation/Foundation/UndoManager/CheckpointMessage)

A message that an undo manager sends at certain checkpoints.

[`DidOpenUndoGroupMessage`](/documentation/Foundation/UndoManager/DidOpenUndoGroupMessage)

A message that an undo manager sends after opening an undo group.

[`WillCloseUndoGroupMessage`](/documentation/Foundation/UndoManager/WillCloseUndoGroupMessage)

A message that an undo manager sends before closing an undo group.

[`DidCloseUndoGroupMessage`](/documentation/Foundation/UndoManager/DidCloseUndoGroupMessage)

A message that an undo manager sends after closing an undo group.

## Relationships

### Conforms To

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)