Day 2 of 100 days of SwiftUI was a quick one.
In this lesson, we covered Boolean values and string interpolation.
Once again, I got JavaScript deja vu. Seriously, if you showed me this:
let name = "The dude"
let action = "abides"
let fullSentence = name + action
print(fullSentence) // "The dude abides"
I would instantly say it's JavaScript. And it is! But it's also Swift!
In fact, the only difference — at least at this introductory level — between the two is how we would use string interpolation.
In Swift, we use \()
to inject variables or methods into a string. In JavaScript, it's ${}
. The latter feels more intuitive to me, but it's literally been less than 15 minutes since I've learned the Swift method, so we'll see how it feels in 98 days.
Instead of using the + symbol to smush the strings together, we can use string interpolation to do the following:
let name = "The dude"
let action = "abides"
let fullSentence = "\(name) \(action)"
print(fullSentence) // "The dude abides"
And ... that was it! Day 2 was a quick one. The lesson was followed by a "checkpoint", which is a small assignment to recap what we've learned so far (strings, integers, interpolation, and so on).
The checkpoint was to save a temperature in celsius to a constant, then convert it to fahrenheit and print the result.
let temp = 33
let tempInFahrenheit = temp * 9 / 5 + 32
print("The temperature in Celsius is \(temp)° and is \(tempInFahrenheit)° in Fahrenheit") // The temperature in Celsius is 33 and 91 in Fahrenheit
On to day 3!