質問

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?

役に立ちましたか?

解決 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;
}

他のヒント

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top