100 Days of SwiftUI Day 5

Day 5

2026-04-22

Now were getting into logic puzzles

Conditions

Comparison operators

  • == is “equal to” because = is used in let to assign a value to a constant

Multiple Conditions

if condition { 
   code 
} else if othercondition {
   othercode
}

I was recently doing Microsoft PowerApps FX code and so I keep wanting commas. Having only the curly brackets will take some getting used to.

Logical operators

  • && the and operator
  • || the or operator

 

ENUM is still pretty weird. I know that it’s an arbitrary fixed list of items, and that its members have type ENUM name but I haven’t quite got my head around it.

So if I write

enum TransportOption {
    case airplane, helicopter, bicycle, car, bus, feet
}

followed by

let transport = TransportOption.airplane

then anytime I use the dot notation for the other members then it automatically knows it’s of type TransportOption.

if transport == .airplane || transport == .helicopter {
    print("Let's fly!")
} else if transport == .bicycle || transport == .car || transport == .bus {
    print("Let's ride!")
} else {
    print("Let's walk!")
}

Switch

All switch statements must be exhaustive, meaning that all possible values must be handled in there so you can’t leave one off by accident.

Swift will execute the first case that matches the condition you’re checking, but no more.

“Must be exhaustive?” Does that mean I have to make an ENUM for switch statements?

< reads more >

Nope, can use default: to handle the cases that weren’t specified.

Huh, I can imagine that I will be totally tripped up by fallthrough.

Ah, he covers it

PS: I’ve covered the fallthrough keyword because it’s important to folks coming from other programming languages, but it’s fairly rare to see it used in Swift – don’t worry if you’re struggling to think up scenarios when it might be useful, because honestly most of the time it isn’t!

Ternary operator

var myVar = condition ? value if true : value if false

It looks like an abbreviated if else statement to me. Paul insists that it will definitely be used in SwiftUI even if it seems redundant now.

Quizzes:

  • Conditions — 12/12
  • Combining conditions — 12/12
  • Switch statements — 6/6
  • The ternary operator — 12/12