Swift Operators & Conditions
Posted on Tue 11 February 2020 in swift
Onward to day 3 of 100 Days of Swift!
Just a few tidbits today. First of all, I'd forgotten about the fallthrough
keyword in a switch
statement. I guess I understand the intent here, people forget to use break
all the time. However, it seems like every other language's switch
statement uses the less safe but more consistent behavior of falling through unless you use break
. If your implementation is technically safer but differs from every other implementation in the world, is it really better? On the other hand, "...because that's how it's always been done" is a crap answer to the legitimate question "why is every switch
statement in the programming world so error prone?"
I'd also forgotten about the very convenient range operators in Swift.
let newbieObsRange = 0..<10
let journeyObsRange = 10..<1000
let powerUserRange = 1000...9000
switch userObsCount {
case newbieObsRange:
print("Welcome!")
case journeyObsRange:
print("Welcome back!")
case powerUserRange:
print("Hey there big observer!")
default:
print("It's over 9000!")
}
Finally, I noticed an annoyance with the Swift Playgrounds app on my iPad. Why does the text editor not know how to indent a switch
statement?