You can use the NSWindowDelegate
protocol in your ViewController class. (See the documentation here)
To make your class conform to the protocol:
class ViewController: NSObject, NSWindowDelegate
To detect when the window's close button has been clicked, use windowShouldClose:
From the doc:
Tells the delegate that the user has attempted to close a window [...]
In this method, you can use NSAlert to prompt the user on whether or not they really want to close the window.
EDIT (in response to @Mr Beardsley's comment)
To make your ViewController the delegate, use:
window.delegate = self
Where self
is the ViewController and window
is the window you're using. You can put this in viewDidLoad:
.