【问题标题】:Document Controller search handling of non file: URLs非文件的文档控制器搜索处理:URL
【发布时间】:2022-01-09 21:33:28
【问题描述】:

具有自定义 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)
}

断言触发!

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

基本上我是在尝试在一个平面命名空间中支持非文件:信息,以提供一致的访问和内容。

【问题讨论】:

    标签: document swift5 nsdocumentcontroller


    【解决方案1】:

    可能不是答案 - 为什么找不到此类 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
        }
    }
    

    享受吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多