【发布时间】:2018-03-18 00:24:46
【问题描述】:
我第一次尝试使用基于 NSDocument 的应用程序。 (Xcode 9.2、Swift 4、macOS 10.12 Sierra 和 Cocoa/AppKit)
我想知道在没有自动保存的情况下实现关闭文档的正确方法。你能告诉我关闭 NSDocument 的最佳做法吗?
当用户试图关闭文档时,会调用下面的 NSDocument 方法。
canClose(withDelegate:shouldClose:contextInfo:)
我调试了参数,发现followimng:
delegate = MyApp.Document
shouldCloseSelector = _something:didSomething:soContinue:
contextInfo = libsystem_blocks.dylib `_NSConcreteMallocBlock
显然这样的选择器不可用,所以我认为可以在这里更改如下:
override func canClose(withDelegate delegate: Any,
shouldClose shouldCloseSelector: Selector?,
contextInfo: UnsafeMutableRawPointer?) {
let delegate : Any = self
let shouldCloseSelector : Selector = #selector(Document.document(_:shouldClose:contextInfo:))
super.canClose(withDelegate: delegate, shouldClose: shouldCloseSelector, contextInfo: contextInfo)
}
@objc func document(_ document : NSDocument, shouldClose flag : Bool,
contextInfo: UnsafeMutableRawPointer?) {
if document === self, flag {
self.cleanup() // my cleanup method
self.close() // NSDocument.close()
}
}
deinit {
Swift.print(#function, #line)
}
这似乎可行,但我想这不是正确的方法,因为忽略了原始参数(Selector/contextInfo)。
【问题讨论】: