 
 import Cocoa
 
-class MainWindowController: NSWindowController {
+class MainWindowController: NSWindowController, NSSpeechSynthesizerDelegate {
     override var windowNibName: String {
         return "MainWindowController"
     }

    @IBOutlet weak var textField: NSTextField!
    @IBOutlet weak var speakButton: NSButton!
    @IBOutlet weak var stopButton: NSButton!

    override func windowDidLoad() {
        super.windowDidLoad()

    }
    

        if string.isEmpty {
            print("stirng from \(textField!) is empty")
        } else {
            speechSynth.startSpeaking(string)
            isStarted = true
        }
    }
    
    @IBAction func stopIt(sender: NSButton) {
        speechSynth.stopSpeaking()
    }
    
    func updateButtons() {
        if isStarted {
            speakButton.isEnabled = false
            stopButton.isEnabled = true
        } else {
            stopButton.isEnabled = false
            speakButton.isEnabled = true
        }
    }
    
    // MARK: - NSSpeechSynthesizerDelegate
    func speechSynthesizer(_ sender: NSSpeechSynthesizer, didFinishSpeaking finishedSpeaking: Bool) {
        self.isStarted = false
    }
    
    /**Being a delegate 
     
     The first thing to understand about being a delegate is that it is a role – possibly one of many – that an object may take on. In Cocoa, roles are defined by protocols. The specific role of “delegate that serves a speech synthesizer” is defined by the NSSpeechSynthesizerDelegate protocol. To sign up for this role, an object must be an instance of a class that conforms to the NSSpeechSynthesizerDelegate protocol. 
     
    The beauty of having delegate roles defined by protocols (delegation) instead of classes (subclassing) is that any object whose type conforms to the protocol can serve in that capacity. Thus, you can assign critical duties without associating them with any particular object class. Moreover, an object can take on multiple roles as needed.
     
     */
    
}
