Re: The Correct Place to Migrate my NSDocument


Bill Pitcher
 

This works but it’s not right and throws a yucky message

let holdDoc = try NSDocumentController.shared.openUntitledDocumentAndDisplay(false) as! Document
// set the updated data
holdDoc.documentQuestions = self.documentQuestions
// show the new document
holdDoc.makeWindowControllers()
holdDoc.showWindows()
holdDoc.updateChangeCount(.changeDone)
// cancel 
throw NSError(domain: "Application", code: 1002, userInfo: [NSLocalizedDescriptionKey : "File Updated", NSLocalizedRecoverySuggestionErrorKey : "The new Untitled document has the information for " + (self.fileURL?.path)! ] )

From Apple’s - Additional Document Type Considerations
"If you want to automatically convert them to be saved as your new type, you can override the readFrom... methods in your NSDocument subclass to call super and then reset the filename and type afterwards. You should use setFileType: and setFileURL: to set an appropriate type and name for the new document. When setting the filename, make sure to strip the filename extension of the old type from the original filename, if it is there, and add the extension for the new type.”

But from NSDocument
"You cannot use this property to change the document’s format after it has already been opened or saved. This property records only the initial document format used when first opening or saving the file."

Call super what??? "Expected '.' or '[' after ‘super'"

Bill Pitcher
bill@...



On 12/05/2018, at 4:32 PM, Bill Pitcher <bill@...> wrote:

I have an App that needs to be upgraded (thanks App Store) and I want the new App to Open and migrate it’s old documents to the new versions document. With the new File Type and Extension.

All good, I can open both types in "override func read(from data: Data, ofType typeName: String) throws”.
And the internal data is all good.

But when it saves (Including the automagical save when quitting) the new migrated data gets put into the old file, keeping the old DocumentType and Extension. (If I change the file extension manually in Finder it then opens fine)

I get the feeling that either I’m no doing this in the right place or I’m missing something obvious. 

Any advice or examples greatly appreciated.

cheers
Bill Pitcher
bill@...







Join {cocoa@apple-dev.groups.io to automatically receive all group messages.