O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making VocalTextEdit … Vocal

Logging the text is nice, but the real goal of VocalTextEdit is that the computer will read your users’ text to them. Cocoa provides a class for synthesizing speech, suitably named NSSpeechSynthesizer. Begin by adding a property to your ViewController that is an instance of NSSpeechSynthesizer.

Listing 26.4 Adding an instance of NSSpeechSynthesizer (ViewController.swift)

import Cocoa

class ViewController: NSViewController {

    let speechSynthesizer = NSSpeechSynthesizer() @IBOutlet var textView: NSTextView! @IBAction func speakButtonClicked(_ sender: NSButton) { print("I should speak \(textView.string)") } @IBAction func stopButtonClicked(_ sender: NSButton) { print("The stop button was clicked") } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required