How do I register UndoManager in Swift?

2020-02-05 03:48发布

How do I use UndoManager (previously NSUndoManager) in Swift?

Here's an Objective-C example I've tried to replicate:

[[undoManager prepareWithInvocationTarget:self] myArgumentlessMethod];

Swift, however, seems to not have NSInvocation, which (seemingly) means I can't call methods on the undoManager that it doesn't implement.

I've tried the object-based version in Swift, but it seems to crash my Playground:

undoManager.registerUndoWithTarget(self, selector: Selector("myMethod"), object: nil)

However it seems to crash, even with my object accepts an argument of type AnyObject?

What's the best way to do this in Swift? Is there a way to avoid sending an unnecessary object with the object-based registration?

7条回答
Viruses.
2楼-- · 2020-02-05 03:53

Update 2: Swift in Xcode 6.1 has made undoManager an optional so you call prepareWithInvocationTarget() like this:

undoManager?.prepareWithInvocationTarget(myTarget).insertSomething(someObject, atIndex: index)

Update: Swift in Xcode6 beta5 simplified use of undo manager's prepareWithInvocationTarget().

undoManager.prepareWithInvocationTarget(myTarget).insertSomething(someObject, atIndex: index)

Below was what was needed in beta4:


The NSInvocation based undo manager API can still be used, although it wasn't obvious at first how to call it. I worked out how to call it successfully using the following:

let undoTarget = undoManager.prepareWithInvocationTarget(myTarget) as MyTargetClass?
undoTarget?.insertSomething(someObject, atIndex: index)

Specifically, you need to cast the result of prepareWithInvocationTarget() to the target type, although remember to make it optional or you get a crash (on beta4 anyway). Then you can call your typed optional with the invocation you want to record on the undo stack.

Also make sure your invocation target type inherits from NSObject.

查看更多
Deceive 欺骗
3楼-- · 2020-02-05 03:57

OS X 10.11+ / iOS 9+ Update

(Works the same in Swift 3 as well)

OS X 10.11 and iOS 9 introduce a new NSUndoManager function:

public func registerUndoWithTarget<TargetType>(target: TargetType, handler: TargetType -> ())

Example

Imagine a view controller (self in this example, of type MyViewController) and a Person model object with a stored property name.

func setName(name: String, forPerson person: Person) {

    // Register undo
    undoManager?.registerUndoWithTarget(self, handler: { [oldName = person.name] (MyViewController) -> (target) in

        target.setName(oldName, forPerson: person)

    })

    // Perform change
    person.name = name

    // ...

}

Caveat

If you're finding your undo isn't (ie, it executes but nothing appears to have happened, as if the undo operation ran but it's still showing the value you wanted to undo from), consider carefully what the value (the old name in the example above) actually is at the time the undo handler closure is executed.

Any old values to which you want to revert (like oldName in this example) must be captured as such in a capture list. That is, if the closure's single line in the example above were instead:

target.setName(person.name, forPerson: person)

...undo wouldn't work because by the time the undo handler closure is executed, person.name is set to the new name, which means when the user performs an undo, your app (in the simple case above) appears to do nothing since it's setting the name to its current value, which of course isn't undoing anything.

The capture list ([oldName = person.name]) ahead of the signature ((MyViewController) -> ()) declares oldName to reference person.name as it is when the closure is declared, not when it's executed.

More Information About Capture Lists

For more information about capture lists, there's a great article by Erica Sadun titled Swift: Capturing references in closures. It's also worth paying attention to the retain cycle issues she mentions. Also, though she doesn't mention it in her article, inline declaration in the capture list as I use it above comes from the Expressions section of the Swift Programming Language book for Swift 2.0.

Other Ways

Of course, a more verbose way to do it would be to let oldName = person.name ahead of your call to registerUndoWithTarget(_:handler:), then oldName is automatically captured in scope. I find the capture list approach easier to read, since it's right there with the handler.

I also completely failed to get registerWithInvocationTarget() to play nice with non-NSObject types (like a Swift enum) as arguments. In the latter case, remember that not only should the invocation target inherit from NSObject, but the arguments to the function you call on that invocation target should as well. Or at least be types that bridge to Cocoa types (like String and NSString or Int and NSNumber, etc.). But there were also problems with the invocation target not being retained that I just couldn't solve. Besides, using a closure as a completion handler is far more Swiftly.

In Closing (Get it?)

Figuring all this out took me several hours of barely-controlled rage (and probably some concern on the part of my Apple Watch about my heart rate - "tap-tap! dude... been listening to your heart and you might want to meditate or something"). I hope my pain and sacrifice helps. :-)

查看更多
对你真心纯属浪费
4楼-- · 2020-02-05 03:57

I tried for 2 days to get Joshua Nozzi's answer to work in Swift 3, but no matter what I did the values were not captured. See: NSUndoManager: capturing reference types possible?

I gave up and just managed it myself by keeping track of changes in undo and redo stacks. So, given a person object I would do something like

protocol Undoable {
     func undo()
     func redo()
}

class Person: Undoable {

    var name: String {
        willSet {
             self.undoStack.append(self.name)
        }
    }
    var undoStack: [String] = []
    var redoStack: [String] = []

    init(name: String) {
        self.name = name
    }

    func undo() {
        if self.undoStack.isEmpty { return }
        self.redoStack.append(self.name)
        self.name = self.undoStack.removeLast()
    }

    func redo() {
        if self.redoStack.isEmpty { return }
        self.undoStack.append(self.name)
        self.name = self.redoStack.removeLast()
    }
}

Then to call it, I don't worry about passing arguments or capturing values since the undo/redo state is managed by the object itself. So say you have a ViewController that is managing your Person objects, you just call registerUndo and pass nil

undoManager?.registerUndo(withTarget: self, selector:#selector(undo), object: nil)
查看更多
欢心
5楼-- · 2020-02-05 03:59

I think it would be Swiftiest if NSUndoManager accepted a closure as an undo registration. This extension will help:

private class SwiftUndoPerformer: NSObject {
    let closure: Void -> Void

    init(closure: Void -> Void) {
        self.closure = closure
    }

    @objc func performWithSelf(retainedSelf: SwiftUndoPerformer) {
        closure()
    }
}

extension NSUndoManager {
    func registerUndo(closure: Void -> Void) {
        let performer = SwiftUndoPerformer(closure: closure)
        registerUndoWithTarget(performer, selector: Selector("performWithSelf:"), object: performer)
        //(Passes unnecessary object to get undo manager to retain SwiftUndoPerformer)
    }
}

Then you can Swift-ly register any closure:

undoManager.registerUndo {
    self.myMethod()
}
查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-05 04:07

I tried this in a Playground and it works flawlessly:

class UndoResponder: NSObject {
    @objc func myMethod() {
        print("Undone")
    }
}

var undoResponder = UndoResponder()
var undoManager = UndoManager()
undoManager.registerUndo(withTarget: undoResponder, selector: #selector(UndoResponder.myMethod), object: nil)
undoManager.undo()
查看更多
够拽才男人
7楼-- · 2020-02-05 04:10

setValue forKey does the trick for me on OS X if one needs to support 10.10. I couldn't set it directly cause prepareWithInvocationTarget returns a proxy object.

@objc
enum ImageScaling : Int, CustomStringConvertible {
    case FitInSquare
    case None

    var description : String {
        switch self {
        case .FitInSquare: return "FitInSquare"
        case .None: return "None"
        }
    }
}
private var _scaling : ImageScaling = .FitInSquare
dynamic var scaling : ImageScaling {
    get {
        return _scaling
    }
    set(newValue) {
        guard (_scaling != newValue) else { return }
        undoManager?.prepareWithInvocationTarget(self).setValue(_scaling.rawValue, forKey: "scaling")
        undoManager?.setActionName("Change Scaling")
        document?.beginChanges()
        _scaling = newValue
        document?.endChanges()
    }
}
查看更多
登录 后发表回答