Trouble Understanding How to Detect Synthesizer Stopped


My attempt at trying to determine if the NSSpeeachSynthesizer has stopped is

    @IBOutlet var stopButton: NSButton!
    @IBOutlet var speakButton: NSButton!
    speechSythesizer.delegate = {
        speechSynthesizer(_ sender: NSSpeechSynthesizer,
            didFinishSpeaking finishedSpeaking: Bool){
                if finishedSpeaking {
                    stopButton.enabled = false
                    speakButton.enabled = true

If I understand the principle, the speechSynthesizer’s delegate needs an implementation of a function to handle the finished speaking. However, I’m having trouble working out the correct Swift syntax.

Any assistance would be greatly appreciated.


First modify class ViewController to read

class ViewController: NSViewController, NSSpeechSynthesizerDelegate

then reenter the deleted function viewDidLoad just to enable the speech synthesizer to delegate

override func viewDidLoad() {
    speechSynthesizer.delegate = self

then when the synthesizer stops/completes, it calls
func speechSynthesizer(sender: NSSpeechSynthesizer, didFinishSpeaking: Bool) {
// put your code here


I had been able to figure out adding the NSSpeechSynthesizerDelegate protocol to the class to make the options available and Xcode did help with presenting the function in a popup as I started to type (good thing or I probably wouldn’t have figured that out) and I was able to figure out the conditional to put inside of that function once it was there. And I could even figure out that to interact with the properties of the buttons I would need some IBOutlet(s). But I’m really baffled as to how I was supposed to know to return the override func viewDidLoad() or how I was supposed to figure out that I needed to type “speechSynthesizer.delegate = self”. I mean, it was apparent that the function alone was not sufficient (and in fact, that the function NEVER ran if I put a breakpoint in so obviously something more was needed to get the ball rolling), but without looking here I’m not sure I would have figured this out. What am I missing? Is there something in one of the prior chapters of the book that explained about this and I just read over it without really understanding the implication of what it was saying and I need to go back and reread?


Most likely you have missed a few important points while reading the book.

Anyone with good programming experience should be able to follow BNR books without any difficulties because they are well written books.

I recommend that you go back and start reading from chapter 1 again, and make sure that you understand the material.


Ibex10. Well, that is what I thought which is why I asked if there is something particular in a prior chapter(s) that explained it I might have missed. This book actually does say:

So your comment about “anyone with good programming experience” seems to be at odds with what this book is saying. And in fact, in the early chapters the authors actually do a very good job of going into great detail about everything and in most cases the answers to challenges are things that have already been covered in that chapter or prior chapters in some form or another. It is only in later chapters that they sometimes have left me scratching my head wondering how they got from one point to another in some rare instances. So yes, maybe if I had some prior programming experience it would help and I would intuitively know to do something that they have not covered in this book, but that shouldn’t be necessary (except perhaps in chapter 28)

Again, if someone can point out a particular chapter or section that has some clue to this I might have missed, I’ll be happy to go back and re-read it again (as I have multiple times to jog my memory about something that I vaguely recall but couldn’t quite remember all the details on since it is all still so new to me). In this particular case though… I don’t recall seeing anything like this in prior chapters and even a search of the iBook hasn’t brought up anything that would seem to explain it. And if there isn’t actually something that I missed in the book that would have helped me with this, then I feel it is rather important for the authors to know about it and consider that on any subsequent updates to the book or new books as it might be helpful toward meeting their goal in this book for first-timers, and even if there is, perhaps the fact I missed it might prompt them to explain or highlight it in some other way to make future revisions even better.


[quote]Ibex10. Well, that is what I thought which is why I asked if there is something particular in a prior chapter(s) that explained it I might have missed. This book actually does say:

So your comment about “anyone with good programming experience” seems to be at odds with what this book is saying.[/quote]
Although I am a great fan of BNR books, I claim that that’s called marketing hype.

This is what you can do:

  1. Read the book again (I don’'t have it, but I suspect it is only a few hundred pages long;) and

  2. Read Apple’s The Swift Programming Language guide. … g_Language

  3. Do the tutorials that Apple provides for both OS X and iOS programming.

Be patient, this will take some time; you may even find it painful, but no pain no gain :slight_smile:

After that, if you are finding you are still struggling I suggest that you learn to program in a simpler programming language first such as C or C++. Swift is a very sophisticated and still evolving programming language; it is definitely not for beginners, again despite all the marketing hype.


Ibex10, actually coming from a background where I started with HyperCard, switched to SuperCard and have been using that ever since, Swift actually is by FAR the closest programming language in terms of simplicity and being approachable. I actually did start by reading Apple’s Swift Programming Language guide. It was the first programming language I have looked at that makes sense. With minor changes a lot of the basics of the language are almost exactly like SuperTalk with just a few differences that are easy enough to pick up. There are some more advanced areas though where there isn’t any equivalent in SuperCard so the concepts are brand new to me, not to mention that Xcode is far different from SuperCard’s Runtime Editor or SuperEdit. I had looked in the past at C++ and at Objective-C… they pretty much freaked me out with their strange syntax and complexity.

But Apple’s iBook had some issues for me. They do a lot to explain the new language and there are playgrounds to follow along and try things out and learn from. BUT… 1. There were (I haven’t read the latest revision so can’t say if it has changed) certain crutches they relied on saying “as you know from Objective-C” especially when it came to certain more advanced concepts I have no reference for in terms of what I know in SuperCard/SuperTalk. That was something of a problem since I don’t know Objective-C. Even worse: 2. The book is completely divorced from using Xcode in any sort of app development way. So while the playground stuff is fun and I could follow along, it did nothing to explain how to create apps.

That was a bit discouraging, but I was happy to see some updated (Swift 2 compatible) third-party iBooks starting to appear. In fact, I picked up three of them.

“Swift for Beginners” by Boisy G. Pitre (Peachpit Press). This one actually disappointed me the most I think. The author did a better job of explaining some of the basics of the language but once again it was all about playgrounds and there were a lot of parts where it just skipped over details and left a lot of unanswered questions. Finally at the tail end of the book it just throws the reader into Xcode for building an app… starts with a very simple OS X utility app and when that is done… it proclaims the reader something of an expert after just one short chapter (wait… what?) and then it jumps into iOS with a basic memory game… and then it sort of drops a whole bunch of miscellaneous stuff on you that probably should have been mentioned earlier but the author couldn’t seem to find a way to include in the prior chapters… and then there is a more advanced iOS game to wrap things up.

“Swift OS X Programming for Absolute Beginners” by Wallace Wang (Apress). Honestly… I was pretty impressed with this one. Far better approach with a rather general overview of what “Object Oriented Programming” is about, and then it actually jumps right into Xcode and creating really simple apps. Lots of “we’ll explain some of this in later chapters” but a good approach overall as learning the language is more directly linked with the Xcode developer environment and actual app interfaces and they mostly did explain things as you progress through the chapters. Some playgrounds as well to demonstrate concepts. Very good balance overall. Missing some details here or there and unfortunately it just didn’t go far enough. Still, I think I got a LOT more practical information out of this book. Actually looking into if Apress has something that follows on from this one.

And then of course “Swift Programming - The Big Nerd Ranch Guide” by Matthew Mathias and John Gallagher (Big Nerd Ranch). In terms of structure… same as that first third-party book (so right there I was a bit apprehensive). That said though, this is, by far, the most detailed when it comes to learning the Swift language and it does an extremely good job of that. I didn’t end up with a lot of unanswered questions in each chapter (in fact, I found it answered gaps in the prior two books that I had been left with). The playground stuff in each chapter is actually very well thought out and useful and the twist… the challenges at the end of each chapter to see if I’m actually understanding the stuff I’ve just read and typed along with or if it is going over my head completely. Yes, that actually made things interesting. And hey, at this point having read 3 other books before this one about Swift language it actually has been mostly easy going and a good review of not only what I read in this book, but anything I might have learned from the others. And the chapters about the Swift language itself are very well done… there is nothing in there I can think of that isn’t explained in the chapter you just read or in prior chapters (and yes, there have been a few times I’ve had to go back and reread the chapter or part of a prior one because something didn’t make sense or just hadn’t quite sunk into my memory yet - but it was also apparent what I missed when I went looking). In this book it all builds up nicely. And then comes the actual app building part of the book. I wish there had been more of this throughout the book. They do a pretty good job here but… the final chapters do seem to take on a little different tone and seem far too short. Some of the details explaining things are a bit less than in prior chapters (still far better than that first third-party book I read). But yes, then we come to the point I asked about in the challenge. How was I to know to do this? That is actually a bit surprising given how previous chapters were rather methodical about making sure there was something to go on. So I do agree that maybe there is something in there I missed. I’ve read through that chapter several times now and I don’t see it and I’m not seeing anything in prior chapters of relevance to this either. I remain rather annoyed having to find the answer here on this forum if the answer is sitting there in the book somewhere. Interestingly… the answer is also needed in the next chapter as well for one of the challenges, but again, I only now have that knowledge from this forum topic. I just want to find out why and if I did actually miss something in this chapter or some prior one that explains it (from anyone who actually has read this book). And, if there isn’t actually an answer in this book, perhaps the authors can consider improving on things in a future revision - and I wouldn’t mind if they also maybe could explain it here as well so I don’t have to wait around for a new revision to find out an answer to this one question.


First you have to add NSSpeechSynthesizerDelegate to the class

class ViewController: NSViewController, NSSpeechSynthesizerDelegate {

Then you have to wire the delegate in the viewDidLoad event:

    override func viewDidLoad() {
        speechSynthesizer.delegate = self

Now add the following method:

func speechSynthesizer(_ sender: NSSpeechSynthesizer, didFinishSpeaking finishedSpeaking: Bool) {
    if finishedSpeaking {
        print("finished speaking")


I am coming a bit late in this discussion, but I think that what you wrote to MikeYenko is very arrogant and presumptuous/pretentious from your side! You state: “I recommend that you go back and start reading from chapter 1 again, and make sure that you understand the material.”: that is really unfair and not effective at all in my opinion! You can only understand the “delegate” concept once you have read an iOS Development or Cocoa book and not by simply reread this book (Swift Programming: The Big Nerd Ranch Guide). Sometimes when comparing my solutions (of this book or any other books of The Big Nerd Ranch Guide series) witho those of other programmers feel really surprised in realizing how bad the others program, but instead of criticizing them I try to help them with my hints or solutions: but that is not apparently your case!


Hi MikeYenco, with a bit of delay I just wanted to say that I agree 100% with what you say! I had read several books about about Swift, Objective-C, Cocoa Programming for OS X, iOS Development, etc, and I decided to read this book just to refresh my knowledge about Swift: nevertheless I also got stuck for a while with this bronze challenge (that at the end I solved alone). Therefore in the future do not pay that much attention to certain arrogant and presumptuous persons such as ibex10!