Default Data for New Documents

I have a Document-Based Cocoa Application set up using Core Data and not using Storyboards. In the Document class I have a function that creates default data. I'm currently calling this function in override init(), so every time a document is initialized, it adds the data.


However, I only want to add this data when a new document is created and not at any other time (e.g. when a file is opened). How might I be able to accomplish this?

Replies

There's a special initializer for this:


https://developer.apple.com/documentation/appkit/nsdocument/1515159-init


Note, though, that there may be a layer of complexity on top of this, if your app can import documents of a foreign type. In that case, a new document needs to be created (to import the foreign data into) but you don't want the standard new-document data. The easiest workaround for that case is to set a global flag in the "open file" delegate method, when the incoming file type indicates an import is needed, and do something different in your init(type:) override when you see the global flag.


But for the simplest case, the above method is what you want.