How do I register UndoManager in Swift?

后端 未结 7 975
温柔的废话
温柔的废话 2020-12-09 19:43

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

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

[[un         


        
7条回答
  •  萌比男神i
    2020-12-09 20:16

    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()
        }
    }
    

提交回复
热议问题