ドキュメントのクリーンアップを実行するメソッドを監視NSApplicationWillTerminateNotification
してオーバーライドします(アプリの終了時に呼び出されません!)[NSDocument close]
[NSDocument close]
MyDocument.h:
@interface MyDocument : NSDocument
{
BOOL _cleanedUp; // BOOL to avoid over-cleaning up
...
}
@end
MyDocument.m:
// Private Methods
@implementation MyDocument ()
- (void)_cleanup;
@end
@implementation MyDocument
- (id)init
{
self = [super init];
if (self != nil)
{
_cleanedUp = NO;
// Observe NSApplication close notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_cleanup)
name:NSApplicationWillTerminateNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// I'm using ARC so there is nothing else to do here
}
- (void)_cleanup
{
if (!_cleanedUp)
{
_cleanedUp = YES;
logdbg(@"Cleaning-up");
// Do my clean-up
}
}
- (void)close
{
logdbg(@"Closing");
[self _cleanup];
[super close];
}