如何让基于 NSDocument 的应用程序打开文件夹,但将关联的文档保存到应用程序支持?

发布于 2024-11-30 18:13:02 字数 1105 浏览 2 评论 0原文

在我的基于 NSDocument 的应用程序中,我的文档将与目录关联(它们是目录的索引),

我希望能够根据以下用例打开/保存文档:

  1. 打开目录: a) 如果文件夹中有.myapp文件,打开它 b) 否则查看 ~/Library/Application Support/MyApp 中是否有 HASH-OF-PATH-basename.myapp 文件并打开该文件 b) 否则在内存中创建一个新文档

  2. 照常打开 xxx.myapp 文件

  3. 保存/自动保存 a) 如果当前文档的源是 1b、1c 或 2,则将文件保存到 ~/Library/Application Support/MyApp/HASH-OF-PATH-basename.myapp b) 如果当前文档的源是 1a,则保存到该 .myapp 文件

  4. 另存为/另存到 b) 将 xxxxx.myapp 保存到用户指定的任何位置

显而易见的事情似乎只是实现 readFromURL:ofType:error:L 和 writeToURL:ofType:forSaveOperation:originalContentsURL:error: 并根据保存操作的内容根据需要切换 URL 。

这是因为我可以打开文件和文件夹,当我保存打开的文件夹时,我的文件将保存到应用程序支持,但在 writeToURL:ofType:forSaveOperation:originalContentsURL:error 之后,会引发错误并显示一个包含以下错误的对话框:

NSDocument could not delete the temporary item at
file://localhost/private/var/folders/6P/6PNpIB-6HreGE+Ikqf5dWU+++TI/
-Tmp-/TemporaryItems/(A%20Document%20Being%20Saved%20By%20MyApp%203)
/SomeDirectory. 
Here's the error:
Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x200583a00 
"“SomeDirectory” couldn’t be removed."

In my NSDocument based app, my documents are to be associated with directories (they are an index of the directory)

I want to be able to open/save documents according to the following use case:

  1. Opening a directory:
    a) if there is a .myapp file in the folder, open it
    b) else look if there is HASH-OF-PATH-basename.myapp file in ~/Library/Application Support/MyApp and open that
    b) else create a new document in memory

  2. Opening an xxx.myapp file, as normal

  3. Save / Autosave
    a) if source of current doc was 1b, 1c, or 2 save file to ~/Library/Application Support/MyApp/HASH-OF-PATH-basename.myapp
    b) if source of current doc was 1a, save to that .myapp file

  4. Save As / Save To
    b) save an xxxxx.myapp to wherever the user specifies

The obvious thing seemed to be just to implement readFromURL:ofType:error:L and writeToURL:ofType:forSaveOperation:originalContentsURL:error: and switch URL as necessary based on what the saveoperation was.

This works in that I can open files and folders, and when I save an open folder my file is saved to Application Support, but then after writeToURL:ofType:forSaveOperation:originalContentsURL:error an error is raised and a dialog shown with the following error:

NSDocument could not delete the temporary item at
file://localhost/private/var/folders/6P/6PNpIB-6HreGE+Ikqf5dWU+++TI/
-Tmp-/TemporaryItems/(A%20Document%20Being%20Saved%20By%20MyApp%203)
/SomeDirectory. 
Here's the error:
Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x200583a00 
"“SomeDirectory” couldn’t be removed."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2024-12-07 18:13:02

事实证明,这个问题很容易解决。

我覆盖了 saveToURL:ofType:forSaveOperation:error: 并执行了 url &类型交换基于 SaveOperation 类型。

现在一切都按预期进行。

This turned out to be fairly easy to resolve.

I overrode saveToURL:ofType:forSaveOperation:error: and did the url & type swapping based on SaveOperation type there.

Now things are working as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文