Hacking With SwiftUI, Day 2

I haven’t written forever, but I feel like doing it may help keep me honest on this little self-improvement journey I’ve undertaken. I just started the delightful 100 Days of SwiftUI project by Paul Hudson, and I’m really digging it so far. He takes an overwhelming concept – learning Swift and SwiftUI from scratch – and, at least so far, breaks it down into easily digestible chunks that you just take one day at a time. It’s the classic “how do you eat an elephant” problem, but specifically for teaching a novice how to code.

Day 2’s lesson was shorter than Day 1, only focusing on Booleans and string interpolation. Importantly, though, this was the first “checkpoint”, which is a mini-review of the concepts covered so far. I vaguely remember some of these concepts from when I’ve toyed with code before, so today’s task was fairly easy.

Your goal is to write a Swift playground that:

  • Creates a constant holding any temperature in Celsius.
  • Converts it to Fahrenheit by multiplying by 9, dividing by 5, then adding 32.
  • Prints the result for the user, showing both the Celsius and Fahrenheit values.

Fairly straightforward, no?

Creating the initial constant is easy enough:

let celsius = 15.0

Creating the constant for the Fahrenheit conversion is also easy enough now that we have the Celsius constant. Some simple mathematical operators looks like this:

let fahrenheit = celsius * 9 / 5 + 32

I still get slightly tripped up by what does and does not require parentheses, but I think I’ll get used to it as I progress further. From what I can grok so far, I think I only need to do the slash + parentheses thing when trying to embed the value inside a string (see below).

The only remaining task is to print the output in a human-friendly format. I tossed in a degree symbol (°) for good measure and voila:

print("The current temperature is \(celsius)°, which is \(fahrenheit)° Fahrenheit." )

A quick click of the build and run button in Xcode and you get:

The current temperature is 15.0°, which is 59.0° Fahrenheit.

Easy enough. Let’s see what tomorrow brings.

Oh no.