【问题标题】:Always appending file extensions in NSDocument总是在 NSDocument 中附加文件扩展名
【发布时间】:2014-10-15 08:10:05
【问题描述】:

在基于 NSDocument 的 Cocoa 应用程序中,始终确保保存到磁盘(或 iCloud)的文件包含我们文档格式的默认文件扩展名的最佳方法是什么?

背景:
用户可以将旧文件加载到仍使用 Type-Creator-Codes 的应用程序中。

在我们的应用程序中启用自动保存后,我们需要确保文件在使用 Cocoa 应用程序写回磁盘(进行任何类型的更改之后)后始终添加文件扩展名 - 否则应用程序不会无法打开它(现在既没有类型创建者代码也没有文件扩展名)。

【问题讨论】:

    标签: objective-c cocoa file-type nsdocument nsapplication


    【解决方案1】:

    如果我做对了,我会覆盖NSDocumentController's open 方法

    - (void)openDocumentWithContentsOfURL:(NSURL *)url 
                                  display:(BOOL)displayDocument 
                        completionHandler:(void (^)(NSDocument *document, 
                                                    BOOL documentWasAlreadyOpen, 
                                                    NSError *error))completionHandler {
        if(!url.pathExtension isEqualToString:@"XYZ")
            url = url URLByAppendingPathExtension:@"XYZ"];
        [super openDocumentWithContentsOfURL:url 
                                     display:displayDocument 
                           completionHandler:completionHandler];
    }
    

    注意:内联编写

    【讨论】:

    • 嗯,有趣 - 我们不能以类似的方式从 NSDocument 获取 URL 吗?喜欢改写readFromURL:ofType:error:当前使用readFromData:ofType:error:)?我们需要直接去NSDocumentController吗?
    • 是的,文档会知道其 url,但控制器仍会将其与旧 url 关联。你真的应该覆盖控制器
    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2015-12-12
    • 2020-04-18
    • 2021-08-29
    相关资源
    最近更新 更多