Errata in Swift Programming, 2nd Ed

Find a typo or error? Note it here!

Tiny typo in Chapter 15 Listing 15.24 ?
I suppose the 16th line in Listing 15.24
let changeName = Person.change
should be
let changeName = Person.changeTo
Just like it’s in the previous page.

Chapter 23: Protocol Extensions

###Page 315:
The text makes references to TreadmillWorkout() knowing how many miles are run, however the struct only has definitions for how many laps were run. miles isn’t anywhere in the text, the code examples, or in the code solution files, except in the text and the output its supposedly generating (although the code above it only references laps in its description).

Chapter 12: Functions (Function Types)

You can even use this constant to call the function; for example, evenOddFunction(numbers: [1, 2, 3]) will sort the numbers…

Calling evenOddFunction(numbers: [1, 2, 3]) yields the following error:

error: extraneous argument label 'numbers:' in call

It seems function types do not support argument labels.

This is a typo in the book. However, the error is not in function types not supporting named parameters, but rather that the function definition sortedEvenOddNumbers specifies that the argument is passed without a name. This is how we can call print() without passing a named argument. Trying sortedEvenOddNumbers(numbers: [1,2,3,4]) will issue the same error.

That said, the book should read:
evenOddFunction([1, 2, 3])

Thank you for the explanation Joshua. I understood the nature of the error when I encountered it. Just wanted to share with the BNR team.

Chapter 16 - Listing 16.6

Error type: bug in example code
Solution: remove the “=” in the computed property declaration

Before:

...
lazy var townSize: Size = {
    get {
        switch self.population {

After:

...
lazy var townSize: Size {
    get {
        switch self.population {

lazy should be lazy

Chapter 13

Chapter 13 on Closures. I have the Kindle book. So, I don’t know exactly what page it is. Two paragraphs above Firgure 13.1 is the statement: “Next, you call sorted( by:), passing in sortAscending(_: _: ) for the second argument.” There is no second argument. The closure is passed to the first argument. Following Listing 13.3 is this statement as well: “Instead of providing a function defined elsewhere in the playground, you implement a closure inline in the sorted( by:) method’s second argument.” Again, there is no second argument. The closure is passed as the first argument.

Chapter 12: Functions, page 120

Teeny-tiny typo in the 4th paragraph.

Most of the page refers to areaOfTriangleWith( base:height: ).
But the 4th paragraph ends with

…are enclosed by the scope of areaOfTriangle( withBase:andHeight: ).

Thanks! We’ll get this fixed.

Correct! Thanks for catching. I believe this typo is holdover from the previous syntax.

Actually, the = is struck through; it’s just hard to see because of all the lines. Apologies for that confusion.

Thanks. We will get this fixed.

Chapter 21 Extensions

A minor point which doesn’t affect the code, but irritates the physicist in me. :slight_smile:

Velocity is used as a type alias for Double. Velocity is then used to set up a variable topSpeed.

However, speed is a scalar, not a velocity. Velocity is a vector with magnitude and direction.

Velocity should be renamed Speed.

I should also say that it is a great book. It’s renewed my enthusiasm for learning Swift.

Page 417 Listing 19.9, I don’t know whether you’ve already seen or whether this is a typo but I had to change it to get it to work for me.

var department = Department(name: "Engineering")
department.addPerson(Person(name: "Joe", age: 30, yearsOfExperience: 6))
department.addPerson(Person(name: "Karen", age: 40, yearsOfExperience: 18))
department.addPerson(Person(name: "Fred", age: 50, yearsOfExperience: 20))

Should be:

var department = Department(name: "Engineering")
department.add(Person(name: "Joe", age: 30, yearsOfExperience: 6))
department.add(Person(name: "Karen", age: 40, yearsOfExperience: 18))
department.add(Person(name: "Fred", age: 50, yearsOfExperience: 20))

Hope this helps.

Page 114:
Maybe the title of the listing should read:
Listing 12.4 Using external parameter names
rather than:
Listing 12.4 Using explicit parameter names

That’s a fair point. We’ll make sure to address this in the next edition.

Thanks for the feedback!

Thanks, the prose and code output should be referring to laps instead. We’ll get this fixed.

Yep, definitely a typo. Thanks!

Chapter 13 Closure Syntax in paragraph below code listing 13.2 is the sentence:
“The function will return true if i is less than – and should be placed before - j.”

I think it should read

“The function will return true if i is less than j and should be placed before j.”