Tuesday, June 25, 2024
HomeIOS Developmentif case let in Swift defined – Donny Wals

if case let in Swift defined – Donny Wals


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 might need seen a case together with an if assertion.

On this submit, we’ll discover totally different locations the place we are able to use the case key phrase to carry out one thing known as sample matching in Swift.

Sample matching is a strong characteristic of Swift that permits us to carry out extremely elegant checks to see if a given kind matches a sure worth.

On this submit, we’ll discover a particular type of sample matching in Swift; the if case let method 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 nicely 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 approach.

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 more than likely the one you’re most acquainted with:

swap myShape {
case .rectangle:
  print("myShape is a rectangle")
case .triangle:
  break
case .circle:
  break
}

A swap in programming permits us to jot down an inventory of patterns that we need to evaluate a given worth to. That is rather more handy that writing a bunch of if / else statements.

The case key phrase in Swift doesn’t carry out any particular magic. As a substitute, 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 provide help to perceive if case let syntax?

Understanding if case let

As soon as you understand that if case .rectangle = myShape invokes a comparability between .rectangle and myShape the next immediately makes a little bit extra sense:

enum LoadingState {
  case inProgress(Job<String, By no means>)
  case loaded(String)
}

let state = LoadingState.loaded("Hey, 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 evaluate our enum case of .loaded and we assign its related worth to a relentless. I want case .loaded(let string) myself as a result of it seems 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 job):
  break
case .loaded(let string):
  print("Loaded string is (string)")
}

Once more, the sample right here is that we evaluate our case to a worth. In a swap this seems much more pure than it does in an if assertion however they’re the identical below the hood they usually each use the underlying ~= comparator.

That mentioned, writing if case .loaded(let string) = state once you’re solely interested by a single case is actually extra handy than writing a full blown swap once you’re solely interested by a single case.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments