<!--
{
  "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

[`func registerUndo<TargetType>(withTarget: TargetType, handler: (TargetType) -> Void)`](/documentation/Foundation/UndoManager/registerUndo(withTarget:handler:))

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

[`- (void) registerUndoWithTarget:(id) target handler:(void (^)(id)) undoHandler;`](/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.

[`func registerUndo(withTarget: Any, selector: Selector, object: Any?)`](/documentation/Foundation/UndoManager/registerUndo(withTarget:selector:object:))

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

[`func prepare(withInvocationTarget: Any) -> Any`](/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

[`var canUndo: Bool`](/documentation/Foundation/UndoManager/canUndo)

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

[`var canRedo: Bool`](/documentation/Foundation/UndoManager/canRedo)

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

### Performing undo and redo

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

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

[`func 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.

[`func 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

[`var levelsOfUndo: Int`](/documentation/Foundation/UndoManager/levelsOfUndo)

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

[`var undoCount: Int`](/documentation/Foundation/UndoManager/undoCount)

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

[`var redoCount: Int`](/documentation/Foundation/UndoManager/redoCount)

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

### Creating undo groups

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

Marks the beginning of an undo group.

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

Marks the end of an undo group.

[`var groupsByEvent: Bool`](/documentation/Foundation/UndoManager/groupsByEvent)

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

[`var groupingLevel: Int`](/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

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

Disables the recording of undo operations.

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

Enables the recording of undo operations.

[`var isUndoRegistrationEnabled: Bool`](/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

[`var isUndoing: Bool`](/documentation/Foundation/UndoManager/isUndoing)

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

[`var isRedoing: Bool`](/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

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

Clears the undo and redo stacks and reenables the manager.

[`func removeAllActions(withTarget: Any)`](/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

[`var undoActionName: String`](/documentation/Foundation/UndoManager/undoActionName)

The name identifying the undo action.

[`var redoActionName: String`](/documentation/Foundation/UndoManager/redoActionName)

The name identifying the redo action.

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

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

[`func setActionName(String)`](/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

[`var undoMenuItemTitle: String`](/documentation/Foundation/UndoManager/undoMenuItemTitle)

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

[`var redoMenuItemTitle: String`](/documentation/Foundation/UndoManager/redoMenuItemTitle)

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

[`func undoMenuTitle(forUndoActionName: String) -> String`](/documentation/Foundation/UndoManager/undoMenuTitle(forUndoActionName:))

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

[`func redoMenuTitle(forUndoActionName: String) -> String`](/documentation/Foundation/UndoManager/redoMenuTitle(forUndoActionName:))

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

### Working with user info

[`func setActionUserInfoValue(Any?, forKey: UndoManager.UserInfoKey)`](/documentation/Foundation/UndoManager/setActionUserInfoValue(_:forKey:))

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

[`func undoActionUserInfoValue(forKey: UndoManager.UserInfoKey) -> Any?`](/documentation/Foundation/UndoManager/undoActionUserInfoValue(forKey:))

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

[`func redoActionUserInfoValue(forKey: UndoManager.UserInfoKey) -> Any?`](/documentation/Foundation/UndoManager/redoActionUserInfoValue(forKey:))

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

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

An extensible namespace for undo and redo user info keys.

### Working with run loops

[`var runLoopModes: [RunLoop.Mode]`](/documentation/Foundation/UndoManager/runLoopModes)

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

[`var NSUndoCloseGroupingRunLoopOrdering: Int`](/documentation/Foundation/NSUndoCloseGroupingRunLoopOrdering)

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

### Using discardable undo and redo actions

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

Sets whether the next undo or redo action is discardable.

[`var undoActionIsDiscardable: Bool`](/documentation/Foundation/UndoManager/undoActionIsDiscardable)

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

[`var redoActionIsDiscardable: Bool`](/documentation/Foundation/UndoManager/redoActionIsDiscardable)

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

### Working with notifications

[`static let NSUndoManagerWillUndoChange: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerWillUndoChange)

Posted just before an undo manager performs an undo operation.

[`static let NSUndoManagerDidUndoChange: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidUndoChange)

Posted just after an undo manager performs an undo operation.

[`static let NSUndoManagerWillRedoChange: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerWillRedoChange)

Posted just before an undo manager performs a redo operation.

[`static let NSUndoManagerDidRedoChange: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidRedoChange)

Posted just after an undo manager performs a redo operation.

[`static let NSUndoManagerCheckpoint: NSNotification.Name`](/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.

[`static let NSUndoManagerDidOpenUndoGroup: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidOpenUndoGroup)

Posted whenever an undo manager opens an undo group.

[`static let NSUndoManagerWillCloseUndoGroup: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerWillCloseUndoGroup)

Posted before an undo manager closes an undo group.

[`static let NSUndoManagerDidCloseUndoGroup: NSNotification.Name`](/documentation/Foundation/NSNotification/Name-swift.struct/NSUndoManagerDidCloseUndoGroup)

Posted after an undo manager closes an undo group.

[`let NSUndoManagerGroupIsDiscardableKey: String`](/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

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

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

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

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

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

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

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

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

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

A message that an undo manager sends at certain checkpoints.

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

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

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

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

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

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

## Relationships

### Conforms To

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

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

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

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

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

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

[`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)