Errata in Swift Programming, 2nd Ed

Chapter 7 Strings: Canonical Equivalence

Indices and ranges

On Page 67 of the print edition, the following sentence appears in the first paragraph.

This property yields the starting index of a string as a String.CharacterIndex.Index.

I believe this should be corrected to be as follows.

This property yields the starting index of a string as a String.CharacterView.Index.

Chapter 22: Generics (Generic Functions and Methods), page 298

Both listing 22.6 (“Your own map function”) and figure 22.1 (“myMap declaration”), which mostly does a great job of breaking down the myMap(_:_:slight_smile: declaration, describe the function thusly:

func myMap<T,U>(_ items: [T], _ f: (T) -> (U)) -> [U] {

It’s the parentheses around U in the second parameter (as in, f: (T) -> (U)) that I question.

Around the T, sure; that’s what indicates a function (or closure) is expected. But around the U? The result of the function is not another function, so…what gives? The code appears to work identically without the extra parens:

func myMap<T,U>(_ items: [T], _ f: (T) -> U) -> [U] {

I’m not understanding why they’re included, or if there’s some case in which they are actually needed.

(Reporting this here under the belief that it’s a typo.)

Chapter 28: Interoperability (Adding an Objective-C Class), page 443

This isn’t so much an erratum as it is a suggestion for clarity in future editions.

The fourth paragraph (supported by Figure 28.23 generateDefaultImageOfSize: in Swift) advises:

Option-click on generateDefaultImage(of:) in NewContactViewController.swift to see how the method is exported to Swift

But that’s useless. At that point, the munging of the ObjC method name has already been done, and somehow we magically knew which name to use! Far more helpful would be to see how we came to know that.

After a fair bit of searching, I came upon a fantastic post on StackOverflow. (See also the links he posted as comments under the original question.)

Instead of the text as presented in the book, how about instructing the readers to select ImageFactory.h in the project navigator, then choose Navigate > Jump to Generated Interface? Boom. This shows exactly how the method is being exported to Swift, but BEFORE we’ve entered the code in the .swift source file. (Navigate > Jump to Original Source to return.)

I verified the technique works in Xcode 8.2.1; I have no idea about other versions.

Chapter 15: Structs and Classes (A New Project), page 168

KurtSchmucker posted in the chapter forum that the term “toolbar” (second line on the page, and in the label for Figure 15.7 “Xcode toolbar”) is misleading, as the reference is actually to the menu bar, as an application’s “toolbar” is normally contained within a window, as in TextEdit.

Chapter13: Closures, page 134

I think that in the 6th paragraph lines 1 and 4

townPlan

should read

townPlanByAddingLightsToExistingLights

The “- and should be placed before -” part is meant to read as a sort of parenthetical aside. I can definitely understand how you were confused by the sentence. We’ll get it cleaned.

You’re correct! Thanks.

Thanks for the suggestion!

Indeed! Nice catch. Thanks.

In Chapter 9, pages 89-90, two arrays are being compared for equality. The note on page 89 says the test will fail due to the order (“Find a triple rainbow” is in the wrong position). But the test also fails on this page and on page 90 (once the order has been corrected) since the first bucketList item differs (“Mr. Everest” versus “Mt. Kilimanjaro”).

Are you missing the line bucketList[0] = "Climb Mt. Kilimanjaro"?

There’s the same problem in Listing 16.6. I guess the “=” is meant to be struck through, but you can’t actually tell this on the Kindle edition.

Listing 17.1 won’t work if you’ve done the Silver challenge in chapter 16 and your Town has a mayor property

Chapter 13 page 142

I’m not getting the book answer of 10844. What am I doing wrong ?

let totalProjection = projectedPopulations.reduce(0) {

(accumlatedProjection: Int, precinctProjection: Int) -> Int in
return accumlatedProjection + precinctProjection

}
totalProjection

Sorry if this seems trivial… Listing 4.21 on page 34 of print edition won’t work because d1 was declared as constant on the previous page. Changing that declaration to “var d1 = 1.1” fixes things.

@olgrenm It should work as written. The line if d1 + 0.1 doesn’t mutate d1.

Hey @holly2015,

What are you getting? Can you post the whole snippet?

For reference, this is the code that I have:

let precinctPopulations = [1_244, 2_021, 2_157]
let projectedPopulations = precinctPopulations.map {
    (population: Int) -> Int in
    return population * 2
}
projectedPopulations

let bigProjections = projectedPopulations.filter {
    (projection: Int) -> Bool in
    return projection > 4_000
}
bigProjections

let totalProjection = projectedPopulations.reduce(0) {
    (accumulatedProjection: Int, precinctProjection: Int) -> Int in
    return accumulatedProjection + precinctProjection
}
totalProjection // 10844

I needed to restart playground and it worked after that

Sorry, I fixed the issue by restarting Xcode and the code finally copied correctly.

I had the same issue. Just as an experiment, I added the array name after “in”, and that works as well:

(population: Int) -> Int in precinctPopulations