cacau
cacau

Reputation: 3646

Always appending file extensions in NSDocument

What would be the best way to always ensure the file saved to disk (or iCloud) contains the default file extension for our document format in an NSDocument based Cocoa app?

Background:
Users can load legacy files into the app that still use Type-Creator-Codes.

With auto-saving enabled in our app we need to make sure the file always has the file extension added as soon as it's written back to the disk (following any kind of changes) with our Cocoa app - or the app won't be able to open it (with now neither the type-creator-code nor the file extension).

Upvotes: 1

Views: 296

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

If I got it right I'd overwrite NSDocumentController's open method

- (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];
}

NOTE: written inline

Upvotes: 2

Related Questions