Question

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?

Was it helpful?

Solution 2

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top