Swift undo-redo manager
Undo and redo are common requirements in interactive applications, especially when working with mutable state that evolves over time.
While Swift makes it easy to mutate values, it does not provide a built-in mechanism to track changes or revert them in a structured way. As a result, undo / redo logic is often implemented manually, tightly coupled to application logic, or handled externally.
swift-resettable aims to provide a lightweight, opt-in mechanism for recording value changes and navigating their history.
Consider a mutable value that changes over time:
struct State {
var value: Int = 0
}
var state = State()
state.value = 1
state.value *= 10
state.value += 1Once these changes are applied, there is no straightforward way to revert to a previous state without manually storing snapshots or writing custom undo logic.
This quickly becomes error-prone as state grows more complex or mutations become more frequent.
Mark a value as resettable to enable undo / redo functionality.
struct State {
var value: Int = 0
}
@Resettable
let state = State()Any subsequent mutations are recorded automatically:
state.value = 1 // value == 1
state.value *= 10 // value == 10
state.undo() // value == 1
state.value += 1 // value == 2
state.undo() // value == 1
state.redo() // value == 2Undo and redo operate on recorded value states, allowing you to navigate mutation history without manual bookkeeping.
You can add Resettable to an Xcode project by adding it as a package dependency.
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/swift-resettable.git"into the package repository URL text field - Choose products you need to link them to your project.
If you use SwiftPM for your project, you can add Resettable to your package file.
.package(
url: "https://github.com/capturecontext/swift-resettable.git",
.upToNextMinor(from: "0.1.0")
)Do not forget about target dependencies:
.product(
name: "Resettable",
package: "swift-resettable"
)This library is released under the MIT license. See LICENCE for details.