Swift 4 (note that previous methods are deprecated):
 window.beginSheet(self.uiSettingsPanel, completionHandler: {response in NSLog("Finished sheet, response: \(response)") }) 
where self.uiSettingsPanel is an instance of an NSPanel subclass. Then in the NSPanel subclass for the sheet, close it with something like
 @IBAction func buttonOK(_ sender: NSButton) { self.sheetParent!.endSheet(self, returnCode: .OK) }