Document Controller对非文件的搜索处理:URL



使用自定义URL方案的全局文档?

我需要通过URL缓存信息,使用自定义方案-非文件:;以允许用户访问,否则将这些URL视为global,因此通过其URL进行的任何访问都会看到相同的数据。这只是访问用户默认值的一种奇特方式。

如果文档退出(以前打开过(,我将依靠文档控制器的document(url:)来找到这样的URL。

但事实并非如此?

考虑一下这个在应用程序中确实完成了启动:

do {
    let ibm = URL.init(string: "https://www.ibm.com")!
    let doc = try docController.makeDocument(withContentsOf: ibm, ofType: "myType")
    assert((doc == docController.document(for: ibm)), "created document is not found?")
} catch let error {
    NSApp.presentError(error)
}

断言着火了!

所以我停下来,试着弄清楚我做错了什么。

从本质上讲,我试图在一个平面命名空间中支持非file:info,以提供一致的访问和内容。

可能不是答案-为什么没有找到这样的URL方案,但一个有效的解决方案是缓存任何东西,用这样的缓存引导搜索方法,但这样做会产生维护问题:

@objc dynamic var docCache = [URL:NSDocument]()
override var documents: [NSDocument] {
    let  appDocuments = Array(Set([Array(docCache.values),super.documents].reduce([], +)))
    
    return appDocuments
}
override func document(for url: URL) -> NSDocument? {
    if let document = super.document(for: url) {
        docCache[url] = document
        return document
    }
    else
    if let document = docCache[url] {
        return document
    }
    else
    {
        return nil
    }
}

享受吧。

最新更新