<!--
{
  "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/registerUndo(withTarget:selector:object:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUndoManager(im)registerUndoWithTarget:selector:object:"
  },
  "title" : "registerUndo(withTarget:selector:object:)"
}
-->

# registerUndo(withTarget:selector:object:)

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

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

## Parameters

`target`

The target of the undo operation.

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

`selector`

The selector for the undo operation.

`object`

The argument sent with the selector.

    The undo manager maintains a strong reference to     `anO``bject`    .

## Discussion

Use [`registerUndo(withTarget:selector:object:)`](/documentation/Foundation/UndoManager/registerUndo(withTarget:selector:object:)) to register a selector for an undo operation.  To register a selector on the undo stack, you also need to make the method available to the Objective-C runtime by applying the @`objc` attribute to the method.  For more on how to create a selector, see [Selectors](https://developer.apple.com/library/archive/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-ID59).

Calling this method also clears the redo stack.

The following example demonstrates how to register and use a selector on the undo stack by modeling a `Garden` class with two methods: `plant(flower:)` and `pluck(flower:)`.  The `plant(flower:)` method removes a flower from the garden while `pluck(flower:)` adds a flower such that effectively, the two methods are inverse operations of each other.  This inverse quality makes it ideal to register `plant(flower:)` and `pluck(flower:)` to be each other’s undo operation.

```swift
class Garden {
    typealias FlowerName = String

    var flowers: [FlowerName: Int]
    var manager = UndoManager()

    init(flowers: [FlowerName: Int]) {
        self.flowers = flowers
    }
    @objc func plant(flower: FlowerName) {
        flowers[flower, default: 0] += 1
        manager.registerUndo(withTarget: self, selector: #selector(pluck), object: flower)
    }
    @objc func pluck(flower: FlowerName) {
        flowers[flower, default: 0] -= 1
        manager.registerUndo(withTarget: self, selector: #selector(plant), object: flower)
    }
}
var garden = Garden(flowers: ["orchid" : 2, "lavender": 1, "violet": 2])
garden.pluck(flower: "orchid")
//garden.flowers == ["orchid" : 1, "lavender": 1, "violet": 2]
garden.manager.undo()
//garden.flowers == ["orchid" : 2, "lavender": 1, "violet": 2]
```

> Important:
> This method raises ``doc://com.apple.foundation/documentation/Foundation/NSExceptionName/internalInconsistencyException`` if it’s called when no undo group has been established using ``doc://com.apple.foundation/documentation/Foundation/UndoManager/beginUndoGrouping()``. Undo groups are usually set by default, and you rarely need to create a top-level undo group explicitly.

## See Also

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

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

  [Introduction to Undo Architecture](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UndoArchitecture/UndoArchitecture.html#//apple_ref/doc/uid/10000010)



---

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)