2

How do you know if a NSDocument is new?

I'm currently using fileURL != nil but I couldn't find any place in the documentation that confirms this.

Also, fileURL returns nil in restored documents (after quitting the app without saving and then opening the app again). Is it possible to differentiate between a new document and a restored document?

hpique
  • 119,096
  • 131
  • 338
  • 476
  • If it's new I need to show a different window. If it's restored I need to disable certain features. – hpique Aug 29 '12 at 21:20
  • By "restored", do you mean the user viewing it in the Versions browser? Or actually after that, when a document has been restored from the browser? – Mike Abdullah Feb 04 '13 at 16:33

2 Answers2

1

The answer is -[NSDocument draft].

https://developer.apple.com/documentation/appkit/nsdocument/1515065-draft

pravdomil
  • 2,961
  • 1
  • 24
  • 38
0

By trial-and-error:

- (BOOL) readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
    self.isNew = NO; // New documents don't call this method

    BOOL success = [super readFromURL:url ofType:typeName error:outError];

    // This must go after calling super
    self.isSaved = self.fileURL != nil; // Saved documents set fileURL
    self.isUnsaved = self.fileURL == nil; // Unsaved/restored documents don't set fileURL. The url parameter points to a temporary folder.

    return success;
}
hpique
  • 119,096
  • 131
  • 338
  • 476