Errata for Cocoa Programming for OS X, 5th Edition
If you encounter issues in the book, please post them to the forum for the appropriate chapter at forums.bignerdranch.com. Thank you!
2. Swift Types
countingUpshould be defined as a variable (and not a constant) in the section Literals and Subscripting:
var countingUp = .... This is necessary because it is later mutated when calling
countingUp.append("three")in the Instance methods section.
3. Structures and Classes
- In the section "Reference and Value Types",
ball0.particle.x = 1should be
ball0.position.x = 1.
speechSynthesizer(_:didFinishSpeaking:)method is correctly implemented to set
isStarted = false. However, later in the chapter the same method is incorrectly shown to call
- In the section "Common errors in implementing a delegate", the description of buggy behavior upon misspelling
speechSynthesizer(_:didFinishSpeaking:)should state that the Start button will never be enabled (the Stop button remains enabled indefinitely). Additionally, the window can not be closed.
8. KVC, KVO, and Bindings
- In the For the More Curious section on Dependent Keys, the class
Personhas two problems: First, as documented earlier in the chapter, a class must subclass NSObject to participate in KVO. Thus, Person should subclass NSObject. Second, the computed property
fullNamemust have the explicit type
14. User Defaults
- In the section Storing the User Defaults, the text says that "you will need to make the app delegate the delegate of the text field". As shown correctly in the code listing that follows, it is the main window controller that should be the delegate of the text field. Later, the text says to open
MainMenu.xib. Instead, you should open
18. Mouse Events
- In the section Improving Hit Detection, the
pressedproperty is used to indicate whether the user actually clicked on the die. The
mouseUp(_:)method (from the previous section entitled Click to Roll), however, does not check
randomize(). As such the if expression in
theEvent.clickCount == 2 && pressed.
25. Auto Layout
- In the section on Ambiguous Layout, on page 375, the reader is instructed to add a call to
visualizeConstraints(_:)at the end of
applicationDidFinishLaunching(_:). The line above, shown for context, is a call to
addConstraints(_:), but it should be
addConstraints(_:)is the old way of doing this; it has been noted as deprecated in Apple's headers.
assertionFailure(). This should be changed to
fatalError(). The reason is that with optimization enabled, such as in a release build, the compiler leaves out assertions. The Swift compiler knows this and in order to ensure that an object is fully initialized, considers it an error. Calls to
fatalError()are not ignored, however.
28. Web Services
In the code snippet demonstrating using
NSXMLDocumentthe result of the calls to
nodesForXPath(_:error:), is cast using
as [NSXMLNode]. Because the type of this API presently returns
[AnyObject]!, in the eyes of the Swift compiler this cast could fail. As such it must be cast using
as!, like this:
nodesForXPath(...) as! [NSXMLNode].
courseFromDictionary(_:)method instantiates an
NSDateFormatterand assigns a var to reference it:
var dateFormatter = NSDateFormatter(). The
dateFormatterreference should never be changed to refer to a different object, so it should be a
let dateFormatter = NSDateFormatter().
ScheduleFetcher, the same error code (
1) is used twice. There should be a different error code for each type of error. The error code for the second error ("Bad status code") should have code
let error = self.errorWithCode(2, localizedDescription: "Bad status code \(response.statusCode)").
29. Unit Testing
- In the section "Refactoring for Testing" where the contents of
fetchCoursesUsingCompletionHandler(_:)is cut and pasted to
resultFromData(_:response:error:), the line
result = .Success()should read
result = self.resultFromData(data). Cutting and pasting avoids this problem.