Welcome to day 2 of the 100 Days of SwiftUI. If you didn't read day 1, head on over to the post here: https://blog.grayw.co.uk/100-days-of-swiftui-day1. But, in short, I'll be following the 100 Days of SwiftUI from hackingwithswift.com, and documenting my progress here (for as long as I remember to).
If you'd like to follow this series, you can do so by following the tag here: https://blog.grayw.co.uk/tag/100daysofswiftui/
With that out of the way, let's get down to it!
Simple Data Types: Part 2
Today looks like a slightly shorter day than yesterday from the list of topics. It looks like we're going to cover booleans, and joining strings. There's also the first checkpoint of the course.
I'll also be working from the MacBook today, rather than the iPad. I did notice that having a video playing PiP (Picture in Picture) from Safari, did utilise a vast amount of the battery. I may have to keep the iPad for just lighter reading. We'll see.
So, my setup today is using Xcode on macOS in a new Day 2 playground.
How to store truth with Booleans
Booleans store a value of either true or false. Apparently, these were named after George Boole, an English mathematician.
We can see the basics of this by creating a variable, and then using the .hasSuffix() we learned about in day 1, to check if the filename ends with a specific string.
In this example, we can see that the filename is wales.jpg, and we're using .hasSuffix() to check to see if it ends with .jpg. This is returning true, because ... it does.
The same also applies for the second example that 120 is a multiple of 3. I will take that as true, because I am well and truly numerically dyslexic. They make no sense to me at all.
On the note of numbers making no sense, I did learn about the Japanese method of multiplication a few years ago which made far more sense to me than years of anything else that people tried to drum in to my head.
You can read more about it here: https://www.whizz.com/en-us/blog/how-the-japanese-multiplication-method-works/
Back on Track
Back to Booleans.
Unlike strings, integers, and doubles, booleans do not have a selection of operators as this makes little sense. However, there is one, the !. This will swap the value, as can be seen in the below screenshot.
isAuthenticated starts off as false. We then take that and assign it a new value that is "not" the original value, thereby making it true. Doing the same again, swaps it back and saves that value back to the variable.
Another method is using .toggle(). It achieves the same outcome, but with less code. gameOver starts as false, but by typing gameOver.toggle(), we have swapped the value to true.
- Doubles and Booleans Review Test: 6/6
Joining Strings Together
The second part of today is about joining strings together. There are two ways of doing this. The first is the simplest option. You can use the + operator to join two strings together.
However, this is not the most efficient way of doing things as this causes Swift to create temporary strings as it adds each additional part to it. A good example of this is the luggageCode variable. It will first add 1 and 2 together to make "12", then add the 3, to make "123", and so on.
Something you're unable to do here is + a number in to a string. So the below wouldn't work:
let phoneNumber = 123456789 let message = "Give me a call on " + phoneNumber
However, what you could do is tell Swift to treat the integer as a string instead:
let phoneNumber = 123456789 let message = "Give me a call on " + String(phoneNumber)
This isn't the best way to do this, which brings us to the second way, which is string interpolation.
We can drop other values in to a string by using \(varname) within it. This is much easier to read and understand.
So, for our previous example, we would write it as:
let phoneNumber = 123456789 let message = "Give me a call on \(phoneNumber)"
We can also include things like multiplication inside a string with string interpolation.
print("5 x 5 is \(5 * 5)")
For a deeper dive on string interpolation, I should take a look at https://www.hackingwithswift.com/articles/178/super-powered-string-interpolation-in-swift-5-0
- String Interpolation Review Test: 6/6
Summary of Simple Data
- Swift lets us create constants using let and variables with var. Constants should be used when possible.
- Swift's strings contain text. This can be whole words, sentences, paragraphs, or more. These are created by using double quotes at the start and end, or """ at the start and end on their own line for multi-line strings.
- Storing whole numbers are done with integers - Int
- Storing decimal numbers is done with double - Double which is short for Double Point Floating Numbers. These are not 100% accurate.
- Int and Double's have a range of arithmetic operators
- You can store simple true or false values with Booleans - Bool
- String interpolation lets us place data in strings efficiently and easily.
Simple Data - Checkpoint 1
At checkpoint 1, we're asked to create a playground that achieves the following:
- Creates a constant holding any temperature in Celsius
- Converts that temperature to Fahrenheit by multiplying by 9, dividing by 5, then adding 32.
- Prints the result, showing both the Celsius and Fharenheit values.
Below is my solution.
let celsius = 13 let fahrenheit = celsius * 9 / 5 + 32 print("The current temperature is \(celsius), which is \(fahrenheit) in Fahrenheit.")
This returns the string:
The current temperature is 13, which is 55 in Fahrenheit.
In this checkpoint, I have created two constants. One with an Integer, and the second using the first, using operators to create a second Integer. I then used string interpolation to return a string with data in it.
How did this differ from the solution in the video? Well, first was that I should have used a decimal number for the temperature. I had no inclination to show .x in the temperature.
The second was that I could have used Option+Shift+8 to get the ° symbol to use in the string.
If I were to re-write it to line up with that, I would do it like this:
let celsius = 13.0 let fahrenheit = celsius * 9 / 5 + 32 print("The current temperature is \(celsius)°, which is \(fahrenheit)°F.")
This returns the string:
The current temperature is 13.0°, which is 55.4°F.
Thanks for following along in with day 2. I hope you'll join me for day 3.