At the top of page 186, the could be written in a much more Swifty way:
func validateRaise(raiseNumberPointer: AutoreleasingUnsafeMutablePointer<NSNumber>, error outError: NSErrorPointer) -> Bool {
if let raiseNumber = raiseNumberPointer.memory as NSNumber? {
return true
}
else {
let domain = "UserInputValidationErrorDomain"
let code = 0
let userInfo = [NSLocalizedDescriptionKey : "An Employee's raise must be a number."]
outError.memory = NSError(domain: domain, code: code, userInfo: userInfo)
return false
}
}
Explicit tests for nil when optionals are available is not a recommended Swift pattern.
I got for a Swift-2 style:
func validateRaise(raiseNumberPointer: AutoreleasingUnsafeMutablePointer<NSNumber?>) throws {
guard let _ = raiseNumberPointer.memory else {
throw NSError(domain: NSCocoaErrorDomain, code: NSKeyValueValidationError, userInfo: [NSLocalizedDescriptionKey: "An employee's raise must be a number."])
}
}
(I don’t like making variables that are used exactly once. I just inline the result.)
Before the “_” variable name, I still used “raiseNumber,” and the utilities area of the Xcode window confirmed it was a “NSNumber” (without the question mark). I also later confirmed my recollection that there was an existing error domain & code for this problem. I don’t know why custom ones were used in the text; maybe the authors forgot?