Hacking With SwiftUI, Day 5

I managed to get a lesson done on one out of two days this weekend, so I’m counting that as a success. Back in the saddle for Day 5 of my journey through SwiftUI. Today’s series of lessons is all about Conditions!

Conditions

The first lesson today was about testing conditionals, i.e. – if statements. This was probably the most straightforward lesson today, because from here on it gets pretty hectic. Essentially, you just write a simple sentence that says “if something matches this condition, then do this”. It’s pretty easy! An easy example is this:

var mySanity = "gone"

if mySanity == "gone" {
    print("Sorry, dude. You lost your marbles!")
}

Since the variable mySanity is equal to the string “gone”, the codes prints out the message “Sorry, dude. You lost your marbles!”.

Easy enough, though this is yet another example of me constantly getting tripped up by the difference between parentheses, braces, and brackets.

Multiple conditions

The next step in today’s lesson was about testing for multiple conditions, using if else statements. Pretty straightforward expansion on just regular conditions. They seem pretty readable when there are only a couple things to check, but I have a feeling the next block of this lesson is going to be about a better way to solve for situations with lots of conditions to check because I can see this getting unwieldy already.

Switch

From here, we moved on to switch statements. Switch statements are designed specifically for enums, and they must be exhaustive. You can set a default case that handles anything not explicitly covered in the cases you’re writing, though, so you don’t have to make a case for literally every possible scenario. By default switch statements also stop at the first case that runs, but you can set fallthrough after your statements to have it continue running the next cases. Paul has a great example of using switch statements to print out the 12 Days of Christmas:

let day = 5
print("My true love gave to me…")

switch day {
case 5:
    print("5 golden rings")
case 4:
    print("4 calling birds")
case 3:
    print("3 French hens")
case 2:
    print("2 turtle doves")
default:
    print("A partridge in a pear tree")
}

Without fallthrough, it’s going to stop at the first case. By adding fallthrough after each case it will continue running the rest of the code, printing out the entire lyrics as expected. Find of a fun example!

Ternary Operator

Finally, we wrapped it up with a quick runthrough of the ternary operator. The ternary operator is just a way for us to do either/or behavior on a given condition. Scott Michaud has a great tweet about remembering how to use the ternary operator.

This still feels a little superfluous to me, but Paul says that as we get deeper into actually using SwiftUI we’re going to find some situations where ternary operators become critically important. So…hurray, I guess?

Anyway, this was a fairly in-depth lesson about several different pieces of Swift functionality, and it’s kinda neat to already start to see how some of this stuff fits together and might be getting used in apps I use regularly. The examples for switch statements, in particular, have me wondering if that’s how an app like Carrot does some of its functionality based on weather condition.

Tomorrow’s lesson is about loops and then has another checkpoint, so it feels like we’re getting ready to dive into some more complex features of the language pretty quickly. Can’t wait!