In my project I need to be able to tell the difference between documents created by the user and those restored at application launch by restoreStateWithCoder because there are some thing s that need to be done for new documents, but not restored ones. How can I do this?
2 Answers
How about subclassing "NSDocument
" and using that subclass for your document?
Then, you can catch "restoreStateWithCoder
" as it happens and set a unique flag (e.g. a BOOL property) for those documents that are restored from disk and not created fresh via "File -> New" command.
You can also attempt to "method swizzle" "restoreStateWithCoder
", but you have to decide what property to set in which object.

- 88,797
- 17
- 166
- 215
-
I'm already using the NSDocument subclass that came with the project and implementing restoreStateWithCoder in order to implement custom restoration instructions. The problem is that the preparatory actions mentioned in the original post take place in windowControllerDidLoadNib, which happens BEFORE restoration, rendering any custom flags moot. – John Wells Jun 16 '13 at 03:43
-
what things are you doing and why can't you disable or hide them once you realize the file has been restored and isn't brand new? – Michael Dautermann Jun 16 '13 at 03:44
-
The application is actually a web browser, so even though I'm using multiple document architecture, there's no real document. I'm using windowControllerDidLoadNib to create a new tab upon opening a new window and optionally, depending on user preference, loading a home page. This clashes with tab restoration. I could just clear the tab array right after creation when restoring, but that seems awfully inefficient and wasteful. – John Wells Jun 16 '13 at 04:52
[Answering this for Swift, but the general idea works for Objective-C as well]
When a document is brand new, you generally get a call to the following function:
convenience init(type tyepName: String) throws
You could set a flag in that function (say needSpecialHandling = true
, a variable which is originally initialised to false
) to say whether you need some special handling for such cases.
Then in the makeWindowControllers()
function you use that variable to trigger invoking the special code (if true
) the same way you invoked it possibly in the windowControllerDidLoadNib
function.

- 138
- 1
- 5