問題描述
保存 NSDocument 時收到通知 (Get notified when NSDocument is saved)
How can I get notified when NSDocument
is saved, the first time and subsequent times?
I first thought that overriding writeToURL:ofType:error:
would do it, but it appears that this method is also called for temporary autosaves before the document is saved for the first time, and maybe on copy/duplicate operations.
On the other hand, setFileURL:
appears to be called the first time that the document is saved but not on subsequent times.
Is there some kind of Save notification? Or do I have to work around the various border cases of the above methods?
‑‑‑‑‑
參考解法
方法 1:
NSDocument
has writeSafelyToURL::::, which in addition to the target location URL also gets a NSSaveOperationType passed in. This would allow you to filter out autosave operations.
Don't forget to call the super implementation when overriding writeSafelyToURL
.
(by hpique、Thomas Zoechling)