如何实现一个复杂的基于NSDocument的应用程序,该应用程序可以导入和导出非本机文件类型? [英] How to implement a complex NSDocument-based app that imports and exports non-native file types?

查看:125
本文介绍了如何实现一个复杂的基于NSDocument的应用程序,该应用程序可以导入和导出非本机文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个支持非常复杂的操作的应用程序.这类似于Xcode,主要文档由项目窗口表示,其中包含许多条目.磁盘上的文档是一个文件包(表示其下面具有文件和文件夹结构的目录).

I'm working on an app that supports a very complicated set of operations. It's akin to something like Xcode, in that the main document is represented by something like a project window, with numerous entries in it. The document on disk is a bundle (meaning a directory with a file & folder structure underneath).

至少可以导入和导出一种文件类型( .mfst),但它并不旨在直接编辑此文件类型.也就是说,打开此文件类型将导致创建本机文档类型,并将文件内容导入其中.磁盘文件是一个捆绑包( .mproj),其中包含许多文件.

There is at least one file type that it can import and export (.mfst), but it's not intended to directly edit this file type. That is, opening this file type results in a native document type being created, and the contents of the file being imported into it. The on-disk file is a bundle (.mproj) with numerous files contained within it.

从技术上讲,该文件被逐字复制到本机文档包内的正确位置.对该文件中包含的数据所做的任何更改都将保存到捆绑软件的副本中,而不是导入的副本中.

Technically, that file is copied verbatim into the proper location inside the native document bundle. Any changes to the data contained in that file are saved into the copy in the bundle, not the copy that was imported.

该文件类型也可以导出.

That file type can be exported, too.

问题1::此应用是导入类型的查看器还是编辑器?我认为是前者.

Question 1: Is this app a Viewer of the imported type, or an Editor? I think it's the former.

问题2:

Question 2: This question on supporting imported types shows how to implement import via the NSDocument subclass, but it seems that perhaps subclassing NSDocumentController is a good way to go.

一个原因是,如果用户已经导入了.mfst文件,我想抓住这个事实.还有一些其他文件可能与一个打开的文档有关,应该将其导入该文档中而不是创建一个新文件.

One reason for this is that if the user has already imported a .mfst file, I want to catch that fact. There are other files that might be related to an open document, and should import into that document rather than creating a new one.

问题3:不幸的是,要在项目上运行的某些代码要求磁盘上存在某些文件.这意味着,即使响应打开可导入文件类型而创建了新文档,我也需要继续在磁盘上创建捆绑软件并将其复制到该文件中以便可以对其进行操作.我想对用户保持幻想,这是一个无标题,未保存的文档.有可能吗?

Question 3: Unfortunately, some of the code to operate on the project requires that certain files exist on disk. This means that even when a new document is created in response to opening the importable file type, I need to go ahead and create the bundle on disk and copy the file there so it can be operated on. I'd like to maintain the illusion to the user that this is an untitled, unsaved document. Is that possible?

我意识到这与Apple不需要保存的文档的现代"概念背道而驰.也许让所有操作自动保存会更好.我知道我从未能够不明确地保存文档.我实际上不确定最近推荐的UI是什么(Apple似乎已经改变了方向).

I realize this goes against Apple's "modern" notion of documents that don't need to be saved. Maybe it would be better to just have all operations auto-save. I know I've never been comfortable not being able to explicitly save a document. I'm not actually sure what the recommended UI is these days (Apple seems to have reversed course on this).

欢迎提出建议.

推荐答案

  1. 该行为听起来像是原始文件的查看器,因为它无法更改文件.

  1. The behaviour sounds like a Viewer of the original file because it can not alter the file.

对于导出的文件类型,使用UTExportedTypeDeclarations键值将它们添加到应用程序的Info.plist文件中.下面是字体杵的示例. Font Pestle不会打开或编辑CSS,但会导出以下格式:

For exported file types, add these to your application's Info.plist file using the UTExportedTypeDeclarations key value. An example from Font Pestle is below. Font Pestle does not open or edit CSS, but it does export the format:

 <key>UTExportedTypeDeclarations</key>
     <array>
         <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.source-code</string>
                <string>public.utf8-plain-text</string>
            </array>
            <key>UTTypeDescription</key>
            <string>Cascading Style Sheet</string>
            <key>UTTypeIdentifier</key>
            <string>eu.miln.font-pestle.css</string>
            <key>UTTypeReferenceURL</key>
            <string>http://www.w3.org/Style/CSS/</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>css</string>
                </array>
            </dict>
        </dict>

  • 子类化NSDocumentController是检查是否已导入文件的先前导入副本的合理位置.

  • Subclassing NSDocumentController is a reasonable place to check for previously imported copies of the file being opened.

    如果您发现自己在NSDocumentController子类中编写了数百行代码,请重新考虑您的设计.所需的子类应该最少.

    If you find yourself writing hundreds of lines of code in your NSDocumentController subclass, reconsider your design. The subclassing required should be minimal.

    使用NSDocument NSFileWrapper旨在帮助简化基于包的文档的管理. NSDocumentNSFileWrapper将在用户主动保存或需要为文档选择目的地之前处理未命名文档的临时副本.

    NSFileWrapper is designed to help ease the managing bundle based documents. NSDocument and NSFileWrapper will handle saving temporary copies of your untitled documented before the user actively saves or needs to pick a destination for the document.

    我强烈建议您阅读Apple的

    I highly recommend reading Apple's About the Cocoa Document Architecture and following its guidelines as closely as possible. This will save you time and, hopefully, make maintaining your application easier as Apple evolve their underlying frameworks.

    这篇关于如何实现一个复杂的基于NSDocument的应用程序,该应用程序可以导入和导出非本机文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆