In Swift, we are able to use the case
key phrase in a number of locations. Mostly, a case
is utilized in switched however because you’re right here, you may need seen a case
together with an if
assertion.
On this put up, we’ll discover completely different locations the place we are able to use the case
key phrase to carry out one thing referred to as sample matching in Swift.
Sample matching is a robust function of Swift that enables us to carry out extremely elegant checks to see if a given sort matches a sure worth.
On this put up, we’ll discover a particular type of sample matching in Swift; the if case let
strategy of sample matching.
Understanding sample matching
The syntax for if case let
is considerably complicated. So let’s begin with a fast code pattern that demonstrates how one can write an if
assertion that makes an attempt to match an enum case:
enum ShapeType {
case rectangle, triangle, circle
}
let myShape = ShapeType.rectangle
if case .rectangle = myShape {
print("myShape is a rectangle")
}
Now, let me begin by saying we didn’t want to make use of the case
syntax right here. We may have simply as effectively written the next:
if myShape == .rectangle {
print("myShape is a rectangle")
}
Nevertheless, I like the sooner instance as a result of it introduces the case
syntax in a reasonably clear manner.
Now, earlier than I dig in to indicate you the case let
syntax I’d like to check out the type of sample matching in Swift that’s almost certainly the one you’re most accustomed to:
swap myShape {
case .rectangle:
print("myShape is a rectangle")
case .triangle:
break
case .circle:
break
}
A swap
in programming permits us to put in writing a listing of patterns that we need to examine a given worth to. That is way more handy that writing a bunch of if / else
statements.
The case
key phrase in Swift doesn’t carry out any particular magic. As an alternative, it invokes a particular operator that compares our sample (no matter we write after case
) to a price (the worth we’re switching over in a swap).
So… how does that aid you perceive if case let
syntax?
Understanding if case let
As soon as you already know that if case .rectangle = myShape
invokes a comparability between .rectangle
and myShape
the next abruptly makes slightly extra sense:
enum LoadingState {
case inProgress(Job<String, By no means>)
case loaded(String)
}
let state = LoadingState.loaded("Good day, world")
if case .loaded(let string) = state {
print("Loaded string is (string)")
}
// or
if case let .loaded(string) = state {
print("Loaded string is (string)")
}
In each comparisons, we examine our enum case of .loaded
and we assign its related worth to a continuing. I favor case .loaded(let string)
myself as a result of it appears to be like rather less unusual that case let .loaded(string)
however they’re functionally equal.
And in a swap
, you’d use the identical patterns to match in opposition to which at all times helps me to recollect:
swap state {
case .inProgress(let activity):
break
case .loaded(let string):
print("Loaded string is (string)")
}
Once more, the sample right here is that we examine our case
to a worth
. In a swap
this appears to be like much more pure than it does in an if
assertion however they’re the identical beneath the hood they usually each use the underlying ~=
comparator.
That mentioned, writing if case .loaded(let string) = state
if you’re solely taken with a single case is definitely extra handy than writing a full blown swap if you’re solely taken with a single case.