Errata in Swift Programming, 2nd Ed


Hi gc3182, I am equally confused but this apparent errata which is what I am assuming it is. Have you contacted the authors directly about this. I may do this.


@sjlearmonth: Not directly, since they read this forum.


For me, the statement in Listing 26.2, " if let contents = textView.string, !contents.isEmpty", throws an error: "Initializer for conditional binding must have Optional type, not ‘String’"
Separating the two statements solved the problem for me, i.e., “let contents = textView.string” and "if !contents.isEmpty {}… '
Could this be a code difference between Swift 3 and Swift 4 or have I made a bone-headed mistake? Incidentally, this is how the listing reads in the first edition.


I think I got it to work by using optional chaining in the conditional binding, that is, if let contents = textView?.string, !contents.isEmpty


Thanks!. That works for me too.


In 2nd Edition Listing 26.10

let contents = viewController.textView.string ?? ""

gives the error: Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used

Changing to fixes:

let contents = viewController.textView?.string ?? ""


Saving gives the error:

[NSVBSavePanel init] caught non-fatal NSInternalInconsistencyException 'bridge absent'

See here for that fix:


In listing 26.15 the line:

textView.string = newValue

gives the error:

Cannot assign value of type 'String?' to type 'String'

Changing to fixes:

textView?.string = newValue!

Not sure if this is correct though . . .