Errata in Swift Programming, 2nd Ed


#48

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.
Stephen.


#50

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


#51

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.


#52

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


#53

Thanks!. That works for me too.


#54

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 ?? ""

Also

Saving gives the error:

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

See here for that fix:

https://maclovin.org/blog-native/2017/nspersistentdocument-assertion-failure-in-nsvbsavepanel-viewwillinvalidate


#55

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 . . .