error isn’t a String literal, it’s a variable - that’s why the code works. The book is telling you that you can’t do this:
appendErrorCode(400, toErrorString: "The request failed:")
"The request failed:" is a string literal, so the function can’t modify it.
If you want to pass in a string literal like that, then you need a function similar to the one in 12.7, where the value being returned is separate from the value passed in:
func appendErrorCode(_ code: Int, toErrorString errorString: String) -> String {
var result = errorString
if code == 400 {
result += " bad request."
}
return result
}
Then you can say
var error = appendErrorCode(400, toErrorString: "The request failed:")
print(error)
or you could say
var error = "The request failed:"
error = appendErrorCode(400, toErrorString: error)
print(error)
You’d have the flexibility to pass in a string literal or a string variable, whichever you choose.