Swift 3.1 Compiler Warnings for Listing 17.22

Having entered the changes in Listing 17.22, the compiler errors indeed disappear, however I still see Swift compiler warnings for a print() statement.

Thus, for the line:

print("Size: \(myTown?.townSize); population: \(myTown?.population)")

I see the Swift Compiler Warning:

“String interpolation produces a debug description for an optional value; did you mean to make this explicit?”

and two fixes are suggested by Xcode:

“Use ‘String(describing:)’ to silence this warning”


“Provide a default value to avoid this warning”

The first fix looks like this:

print("Size: \(String(describing: myTown?.townSize)); population: \(String(describing: myTown?.population))")

For the second fix to avoid the warning, supplying 0 as a default works for the second portion of the print statement, as in:

print("Size: \(myTown?.townSize ?? <#default value#>); population: \(myTown?.population ?? 0))")

but I’m not sure what a default value would be for the first portion of the statement.

So, what might one enter for “<#default value#>” in the above code line relative to “myTown?.townSize”?

(BTW, after doing a bit of digging, it would seem that these warnings appear due to changes implemented in Swift 3.1. I do realize these are just warnings, but it would be nice to write code as cleanly as possible, yes?)



Hey Jim,

Found a good overview/discussion of this here: https://stackoverflow.com/questions/42543007/how-to-solve-string-interpolation-produces-a-debug-description-for-an-optional.

Cheers ~ Scott

You could arbitrarily provide something like \(myTown?.townSize ?? Town.Size.small), which would default myTown.townSize to .small if the former is nil.