Challenge: Reset Preferences

Task: “Add a button that will remove all the user’s defaults. Label the button Reset Preferences. Do not forget to update the window to reflect the new defaults.”

Retrieve your Bundle Identifier from your Target > SpeakLine > General > Bundle Identifier

class PreferenceManager {
...
	func resetDefaultPreferences() {
		userDefaults.removePersistentDomainForName("com.bignerdranch.SpeakLine")
		registerDefaultPreferences()
	}
class MainWindowController: NSWindowController {
	// MARK: - Action methods
        ....
	@IBAction func resetPreferences(sender: NSButton) {
		preferenceManager.resetDefaultPreferences()
		textField.stringValue = preferenceManager.activeText!
		setDefaultVoice()

	}

Connect the @IBAction to the button in the XIB

What goes in the missing setDefaultVoice()?

I think I’ve answered my question:

[code]func setDefaultVoice()
{
preferenceManager.activeVoice = NSSpeechSynthesizer.defaultVoice()

    let defaultVoice = preferenceManager.activeVoice!
    
    if let defaultRow = voices.indexOf(defaultVoice)
    {
        let indices = NSIndexSet(index: defaultRow)
        
        tableView.selectRowIndexes(indices, byExtendingSelection: false)
        
        tableView.scrollRowToVisible(defaultRow)
        print("defaultRow = \(defaultRow)")
    }

}[/code]

Alternatively, you could also call the windowDidLoad() method again:

@IBAction func resetPreferences(_ sender: NSButton) {
    preferenceManager.resetDefaultPreferences()
    windowDidLoad()
}

To deselect the selected row in tableView :

tableView.deselectAll( Any ?. self )

Couldn’t be simpler (once you found it in the documentation).