John Wells
John Wells

Reputation: 1149

Differentiating between new documents and restored documents in a Cocoa application

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?

Upvotes: 0

Views: 137

Answers (2)

El Architect
El Architect

Reputation: 138

[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.

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89569

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.

Upvotes: 0

Related Questions