It’s great that you can associate data to Swift’s enums, but sometimes you don’t actually care about the data. It turns out the case statement syntax allows you to just use a bare tag name in that case, which beats the pants off
case let .tag(_, _, _):.
See, if you “don’t care”d all that data out, and then added another bit of associated data, your code would break, because you had the arity wrong: you have three “go away”s, and it expects four:
error: tuple pattern has the wrong length for tuple type '(Int, Int, Int, Int)' case .tag(_, _, _): ^
If all you cared about was, “was this a
.tag?”, then that’s straight up obnoxious. It’s still just
.tag, and you flat-out don’t care what or how much data is bundled with it.
Good news: You can just write this:
Less visual noise, clearer expression of intention, and you’re now safe from arity changes.
If all you care about is the tag, and none of the associated data, write:
and be on your merry way.
Interested in learning more about our basic and advanced iOS Courses?
Learn from the experts at a Big Nerd Ranch Bootcamp!
Interested in leveling up your coding skills from the same authors of the Big Nerd Ranch Guide? Subscribe to The Frontier today!
Juan Pablo Claude