始终确保保存到磁盘(或 iCloud)的文件包含基于 NSDocument
的 Cocoa 应用程序中文档格式的默认文件扩展名的最佳方法是什么?
背景:
用户可以将旧文件加载到仍使用类型创建器代码的应用程序中。
在我们的应用程序中启用自动保存功能后,我们需要确保文件在使用我们的 Cocoa 应用程序写回磁盘(在进行任何类型的更改后)后始终添加文件扩展名 - 否则应用程序将不会'无法打开它(现在既没有类型创建者代码也没有文件扩展名)。
最佳答案
如果我做对了,我会覆盖NSDocumentController的
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];
}
注意:内联编写
关于objective-c - 始终在 NSDocument 中附加文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26377576/