在Swift 3中使用NSUndoManager和.prepare(withInvocationTarget :) [英] Using NSUndoManager and .prepare(withInvocationTarget:) in Swift 3
问题描述
我正在将Xcode 7/Swift 2.2 mac OS X项目迁移到Xcode 8/Swift 3,并且在视图控制器类MyViewController中使用undoManager遇到了问题,该类具有撤消功能.
I am migrating an Xcode 7 / Swift 2.2 mac OS X project to Xcode 8 / Swift 3, and I have run into a problem using undoManager in my view controller class, MyViewController, which has a function undo.
在Xcode 7/Swift 2.2中,此方法运行良好:
In Xcode 7 / Swift 2.2, this worked fine:
undoManager?.prepareWithInvocationTarget(self).undo(data, moreData: moreData)
undoManager?.setActionName("Change Data)
In Xcode 8 / Swift 3, using the recommended pattern from 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")
}
但是,向MyViewController的向下转换始终失败,并且未注册撤消操作.
However, the downcast to MyViewController always fails, and the undo operation is not registered.
我在这里遗漏了明显的东西吗,还是这是一个错误?
Am I missing something obvious here, or is this a bug?
推荐答案
prepareWithInvocationTarget(_:)
(或Swift 3中的prepare(withInvocationTarget:)
)创建一个隐藏的代理对象,Swift 3运行时无法很好地使用它.
prepareWithInvocationTarget(_:)
(or prepare(withInvocationTarget:)
in Swift 3) creates a hidden proxy object, with which Swift 3 runtime cannot work well.
(您可以将其称为错误,然后发送错误报告.)
(You may call that a bug, and send a bug report.)
要达到目的,您不能使用registerUndo(withTarget:handler:)
吗?
To achieve your purpose, can't you use registerUndo(withTarget:handler:)
?
undoManager?.registerUndo(withTarget: self) {targetSelf in
targetSelf.undo(data, moreData: moreData)
}
这篇关于在Swift 3中使用NSUndoManager和.prepare(withInvocationTarget :)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!