- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,NSDocument
子类任务关键型硬件 - 用户真的不想意外关闭文档!因此,我实现了 canCloseDocumentWithDelegate...
以显示 NSAlert
并在关闭前询问。
我现在正在尝试在用 Swift 编写的应用程序中实现同样的事情。
由于答案是异步出现的,因此“应该关闭”结果将传递给委托(delegate)上的回调,而不是简单地返回。在 -canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:
的文档中,它说:
The shouldCloseSelector callback method should have the following signature:
- (void)document:(NSDocument *)doc shouldClose:(BOOL)shouldClose contextInfo:(void *)contextInfo
因此,由于有 3 个不同类型的参数,我无法使用简单的 performSelector:withObject:
样式方法 - 你必须使用 NSInitation。请注意,委托(delegate)的类型是id
,并且上面的签名不会出现在任何正式协议(protocol)中——您不能简单地正常调用该方法。 (请参阅此 mailing list post 以了解如何完成此操作的示例)
现在的问题是,Swift 中不允许 NSInitation!请参阅 Swift 博客 “What Happened to NSMethodSignature” :
Bringing the Cocoa frameworks to Swift gave us a unique opportunity to look at our APIs with a fresh perspective. We found classes that we didn't feel fit with the goals of Swift, most often due to the priority we give to safety. For instance, some classes related to dynamic method invocation are not exposed in Swift, namely
NSInvocation
andNSMethodSignature
.
这听起来像是一件好事,但是当一个简单的 NSDocument
API 仍然需要 NSInitation 时,就会失败!整个问题的真正解决方案是 Apple 引入一个使用 block 回调的新 canCloseDocument...
API。但在此之前,最好的解决方案是什么?
最佳答案
您可以使用一些低级运行时函数来解决此问题:
override func canCloseDocumentWithDelegate(delegate: AnyObject, shouldCloseSelector: Selector, contextInfo: UnsafeMutablePointer<Void>) {
let allowed = true // ...or false. Add your logic here.
let Class: AnyClass = object_getClass(delegate)
let method = class_getMethodImplementation(Class, shouldCloseSelector)
typealias signature = @convention(c) (AnyObject, Selector, AnyObject, Bool, UnsafeMutablePointer<Void>) -> Void
let function = unsafeBitCast(method, signature.self)
function(delegate, shouldCloseSelector, self, allowed, contextInfo)
}
如果您需要将此行为移至另一个方法(例如,在工作表获得用户确认后),只需将委托(delegate)和 shouldCloseSelector 存储在属性中,以便稍后访问它们。
关于objective-c - 如何实现 NSDocument 方法 -canCloseDocumentWithDelegate :shouldCloseSelector:contextInfo: in Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599430/
我喜欢 NSDocument,因为当与核心数据模型结合使用时,它可以免费为我提供具有持久数据的应用程序。但我知道,对于我的特定项目,我只会处理一份“文档”。我不需要文档架构堆栈附带的多文档支持。我仍然
当基于文档的应用程序处于事件状态并且没有打开的窗口时,会自动创建一个新的文档窗口。我想防止这种行为并显示我的“选择模板”对话框,而不是默认创建空白文档。 正如您在屏幕截图中看到的,新文档是在 NSAp
文件的保存和打开效果非常好,但是当选择“恢复到保存”时,在窗口的右侧(以前的文档)一眨眼,我的文档版本就出现了,然后只是空白副本。如果我从这个废话中恢复,副本注释会被更改,但如果我保存并重新打开文档,
我正在创建我的第一个基于 NSDocument 的应用程序。我可以从头开始创建新文档,也可以通过导入旧文件来创建新文档。 这个应用程序将允许每个文档有多个窗口,因此我将覆盖 makeWindowCon
在将 NSObject 添加到 mainmenu.xib 后,我可以使用 [NSApp delegate] 访问应用程序范围的委托(delegate)实例,并设置对象的名称更改为我的 appDeleg
我发现我的基于文档的应用程序的架构存在一个主要问题。基本上是将模型(一个简单的字符串)存储在一个全局变量中,每次字段中的文本发生变化时。我让文档将此字符串保存为数据,并使用此数据恢复重新打开的文件。
我有一个基于 NSDocument 的应用程序,我希望在重新打开文档时保存和恢复我的窗口位置。 Apple 关于此的文档非常少,但我能够拼凑的是,在某些时候,应用程序中的某些内容需要调用 NSWind
我正在用 Swift 编写一个基于 NSDocument 的应用程序。如果应用程序未使用文档参数启动,我已将其设置为打开默认文档。 我注意到,如果我启动该应用程序然后立即打开另一个文档,比如使用“打开
我需要在控制文档 View 的 NSViewController 方法中以编程方式保存基于 NSDocument 的应用程序的事件文档。菜单项通过将 save() 发送给第一响应者来完成此操作。以编程
我有一个基于 NSDocument 的 Cocoa 应用程序,我有几个辅助线程,当用户关闭文档窗口或应用程序退出时,我需要正常终止它们(等待它们运行完当前循环)。我正在使用 canCloseDocum
搜索这篇文章的标题会发现它很常见;确实,我从 Xcode 得到了这个错误。但我似乎找不到任何修复方法。我现在在运行我的程序时看到它,它似乎发生在调用 changeCountTokenForSaveOp
我正在尝试实现保存功能来保存 html 文件。目前,当我尝试保存时它会返回错误。我的代码是 - (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSStri
我的基于文档的应用程序存在内存泄漏。它启动正常,我可以打开或创建新文档,但只能打开一两次,然后应用程序崩溃。我在 Xcode 中使用了分析工具,没有任何问题。 但是,Instruments 揭示了内存
我正在开发一个基本的hello world应用程序,它将打开一个txt文档,编辑它,然后最后保存 文档。处理编辑的最佳实践是什么。 我已经通读了 Apple 的文档,但是当我检查 NSError **
我是一名尝试 Web 浏览器应用程序的新手,正在尝试使用带有 NSDocument 架构的 WebKit(如某些 WebKit 引用代码)。我让它打开一个预先设置的主页,然后将当前 URL 发布到相邻
在其文档中,我的应用程序使用了许多与文档路径相关的资源。因此,在添加 Assets 之前必须保存文档。如何强制调用 [NSDocument saveDocumentAs] ? 我设法完成了其中的一部分
如何正确检索与文档窗口关联的 NSDocument?我需要 NSDocument 作为数据源来在窗口中显示数据。 我是否必须遍历所有打开的文档并检查我的实例是否等于文档的 -windowForShee
我创建了一个基于文档的应用程序,需要打开具有特定扩展名的 xml 文本文件。当我使用 NSDocument 模板在 Xcode 中创建项目时,我指定了所需的扩展名,并且一切正常。 按照 Apple 关
我有一个 NSDocument 类,我需要在其中访问主菜单窗口,即应用程序启动时打开的主菜单窗口。当我从应用程序在该窗口中操作时,一切似乎都正常,但是当尝试从 readFromFileWrapper:
我刚刚开始进行单元测试,我想知道如何正确测试 NSDocument 子类? 在我的测试设置中,我可以初始化文档,但这并不能反射(reflect)文档在应用程序使用时的设置方式,特别是未建立 IBOut
我是一名优秀的程序员,十分优秀!