iai*_*ain 2 cocoa objective-c nsdocument
我的应用程序有一个自定义格式,它在 Info.plist 中注册为编辑器,并且可以读取和写入该格式。它还可以读取许多其他格式,但无法写入。对于这些格式,它在 Info.plist 中注册为查看器。
当我打开其他格式之一时,一切看起来都很好,但是当我保存文件时,我的 NSDocument 会收到一条消息,其中writeToURL:ofType:error:包含我加载的文件的 URL 以及我无法写入的格式的 UTI 类型。
-(NSArray *)writableTypes仅返回我的自定义格式的 UTI,并且-(BOOL)isNativeType:仅返回我的自定义格式的 UTI 的 YES。
我想做的是,就像其他具有本机格式但可以从其他格式读取的应用程序一样,当用户按“保存”时,保存面板将打开,用户选择一个文件名来保存为本机类型。
这是 NSDocument 自己可以做的事情,还是我需要检查一下writeToURL:ofType:error:是否需要手动打开保存面板?
回答我自己,我发现似乎有效的解决方案是readFromURL:ofType:error:重置NSDocument非本机格式的 fileURL、displayName 和 fileType。
- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)error
{
    // Handle loading as normal
    if (![type isEqualToString:kMyNativeType]) {
        [self setFileType:kMyNativeType];
        [self setFileURL:nil];
        // Without this, all non-native files are displayed as Untitled.
        // This sets the window title as the filename without the extension
        NSArray *filenameComponents = [[url lastPathComponent] componentsSeparatedByString:@"."];
        [self setDisplayName:filenameComponents[0]];
    }
    return YES;
}
我不知道这是否是正确的方法,但它似乎适用于我目前想要的,并且似乎是https://developer.apple.com/library/mac/documentation/DataManagement中建议的方法/Conceptual/DocBasedAppProgrammingGuideForOSX/AdvancedTopics/AdvancedTopics.html#//apple_ref/doc/uid/TP40011179-CH7-SW8