Generic Instance Method


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


func registerUndo<TargetType>(withTarget target: TargetType, handler: @escaping (TargetType) -> Void) where TargetType : AnyObject



The target of the undo operation.

The undo manager maintains an unowned reference to the target to prevent retain cycles.


A closure to be executed when an operation is undone.

The closure takes a single argument, the target of the undo operation.


Use registerUndo(withTarget:handler:) to register a closure as an undo operation on the undo stack. The registered closure is then executed when undo is called and the undo operation occurs. The target needs to be a reference type so that its state can be undone or redone by the undo manager.

The following example demonstrates how you can use this method to register an undo operation that adds an element back into a mutable array.

var manager = UndoManager()
var bouquetSelection: NSMutableArray = ["lilac", "lavender"]
func pull(flower: String) {
    manager.registerUndo(withTarget: bouquetSelection) { $0.add(flower) }
pull(flower: "lilac")
// bouquetSelection == ["lavender"]
// bouquetSelection == ["lavender", "lilac"]

To avoid retain cycles with the target, operate on the closure parameter rather than on variables in an outer scope that reference the same target. For example, in the code listing above, the closure operates on the $0 parameter rather than directly on bouquetSelection.

See Also

Registering Undo Operations

func registerUndo(withTarget: Any, selector: Selector, object: Any?)

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

func prepare(withInvocationTarget: Any) -> Any

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