macos – 在Swift 3中使用NSUndoManager和.prepare(withInvocationTarget :)

我正在将 Xcode 7 / Swift 2.2 mac OS X项目迁移到Xcode 8 / Swift 3,我在我的视图控制器类MyViewController中使用undoManager遇到了一个问题,它有一个函数undo.

在Xcode 7 / Swift 2.2中,这很好用:

undoManager?.prepareWithInvocationTarget(self).undo(data,moreData: moreData)
undoManager?.setActionName("Change Data)

在Xcode 8 / Swift 3中,使用https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html推荐的模式

这应该改为:

if let target = undoManager?.prepare(withInvocationTarget: self) as? MyViewController {
    target.undo(data,moreData: moreData)
    undoManager?. setActionName("Change Data")
}

但是,向MycastController的向下转换始终失败,并且未注册撤消操作.

我错过了一些明显的东西,或者这是一个错误

prepareWithInvocationTarget(_:)(或在Swift 3中准备(withInvocationTarget :))创建一个隐藏的代理对象,Swift 3运行时无法正常工作.

(您可以将其称为错误,并发送错误报告.)

为了达到你的目的,你不能使用registerUndo(withTarget:handler :)吗?

undoManager?.registerUndo(withTarget: self) {targetSelf in
    targetSelf.undo(data,moreData: moreData)
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...