gpt4 book ai didi

swift - 实现 canClose 的最佳实践(withDelegate :shouldClose:contextInfo:)

转载 作者:行者123 更新时间:2023-11-28 06:02:04 28 4
gpt4 key购买 nike

我第一次尝试使用基于 NSDocument 的应用程序。(Xcode 9.2、Swift 4、macOS 10.12 Sierra 和 Cocoa/AppKit)

我想知道在没有自动保存的情况下实现关闭文档的正确方法。你能告诉我关闭 NSDocument 的最佳实践吗?

当用户试图关闭文档时,将调用以下 NSDocument 方法。

canClose(withDelegate:shouldClose:contextInfo:)

我调试参数并找到以下内容:

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)。

最佳答案

更新

我找到了解决方案。我希望这会对某人有所帮助。

private var closingBlock : ((Bool) -> Void)? = nil

override func canClose(withDelegate delegate: Any,
shouldClose shouldCloseSelector: Selector?,
contextInfo: UnsafeMutableRawPointer?) {
let obj : AnyObject = delegate as AnyObject
let Class : AnyClass = object_getClass(delegate)!
let method = class_getMethodImplementation(Class, shouldCloseSelector!)
typealias signature =
@convention(c) (AnyObject, Selector, AnyObject, Bool, UnsafeMutableRawPointer?) -> Void
let function = unsafeBitCast(method, to: signature.self)

self.closingBlock = {[unowned obj, shouldCloseSelector, contextInfo] (flag) -> Void in
function(obj, shouldCloseSelector!, self, flag, contextInfo)
}

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 flag {
self.cleanup() // my cleanup method
}

self.closingBlock?(flag)
self.closingBlock = nil
}

override func close() {
Swift.print(#function, #line, #file)
super.close()
}

deinit {
Swift.print(#function, #line, #file)
}

引用信息

https://developer.apple.com/library/content/releasenotes/AppKit/RN-AppKitOlderNotes/

Advice for Overriders of Methods that Follow the delegate:didSomethingSelector:contextInfo: Pattern

https://github.com/DouglasHeriot/canCloseDocumentWithDelegate/blob/master/canCloseDocumentWithDelegate/Document.swift

Shows how to implement the NSDocument method -canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: using Swift. Must use Objective-C to perform an NSInvocation.

关于swift - 实现 canClose 的最佳实践(withDelegate :shouldClose:contextInfo:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49343307/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com