0

I found a major problem with the architecture of my Document based app. Basically a store the model (a simple string) in a global variable, every time the text in field changes. I have the document save this string as it's data, and restore re-opened files using this data.

Now, the major problem that I now see is that if I restore any saved file, I populate the global variable from the document in the documents "readFromData" function (works).

But if I create a new document, "readFromData" is never called, so I have no way to set the global string to "", and thus my new documents global variable is still populated with the last saved string. (I use this to put the string back into the text view on load.

So as a simple workaround, I would need to be able to use a function that is automatically called and only ever called by the creation of a new document, to set my global variable back to "".

I can not find such a function I can override. Does one exist..?

Gary Simpson
  • 2,677
  • 2
  • 17
  • 18

1 Answers1

3

I am not sure I understand what you are trying to do.

You could use this NSDocument initializer:

/* Initialize a new empty document of a specified type,
   and return it if successful.
   …
   You can override this method to perform initialization that
   must be done when creating new documents but should not be done
   when opening existing documents. 
*/
- (instancetype)initWithType:(NSString *)typeName error:(NSError **)outError;

This is invoked exactly once per document at the initial creation of the document. It will not be invoked when a document is opened after being saved to disk.

Pierre Bernard
  • 3,148
  • 2
  • 23
  • 31