NSTextView backed by core data
<p>I have a NSTabViewController with a NSTextView in one tab. The text is saved in core data. I save the text when the view disappears. </p>

<pre><code>override func viewWillDisappear()
{
    super.viewWillDisappear()
    self.saveText()
}
</code></pre>

<p>But how do I save the text when the document itself closes? At the moment I save on every keystroke, but that is probably too excessive. Is there a better way?</p>

<pre><code>func textDidChange(notification: NSNotification)
{
    self.saveText() //save text after every keystroke =&gt; excessive but works
}
</code></pre>

<p><strong>edit:</strong></p>

<pre><code>func saveText()
{
    guard let assumedObject = self.representedObject as? NSManagedObject else { return }
    assumedObject.notes = self.textView.string
}
</code></pre>