Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift "where" key word

Tags:

Disclaimer: swift noob

Hi, I've just begun going learning Swift and was going through The Swift Programming Language (Apple's book released during WWDC) and was wondering what the 'where' keyword is. It was used in

let vegetable = "red pepper" switch vegetable { case "celery":     let vegetableComment = "Add some raisins and make ants on a log." case "cucumber", "watercress":     let vegetableComment = "That would make a good tea sandwich." case let x where x.hasSuffix("pepper"):     let vegetableComment = "Is it a spicy \(x)?" default:     let vegetableComment = "Everything tastes good in soup." } 

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

If anyone could explain and/or show the equivalent in another language (java, c++, obj-c, etc.) that would be great.

Apologies if this has already been asked, I didn't see it anywhere.

like image 354
Alex Avatar asked Aug 16 '14 01:08

Alex


People also ask

Is keyword in Swift?

There are four types of keywords in Swift based on the location of their usage in a swift program.

What is where clause in Swift?

You use where in Swift to filter things, kinda like a conditional. In various places throughout Swift, the “where” clause provides a constraint and a clear indicator of the data or types you want to work with. What's so special about where – as you'll soon see – is its flexible syntax.

What is self keyword in Swift?

In Swift, the self keyword refers to the object itself. It is used inside the class/structure to modify the properties of the object. You can assign initial values to properties in the init() method via self.

What is final keyword in Swift?

The final keyword communicates to the compiler that the class cannot and should not be subclassed. Applying the final modifier to a class definition is a clear signal.


1 Answers

Swift's switch statements are much more powerful than the ones used in most other languages. Instead of simply choosing from a group of values, they use pattern-matching to select which case to evaluate. Let's look at each case from your example:

case "celery": 

Super simple -- this will match the string "celery" and nothing else.

case "cucumber", "watercress": 

This case will match either the string "cucumber" or the string "watercress". Cool. In other languages you'd probably need to use a fallthrough case to get both of these.

case let x where x.hasSuffix("pepper"): 

This case contains two concepts that are particular to Swift switch statements, when compared to Java and C. The first is value-binding -- the let x part of this statement binds the matched value to a constant x that is scoped to the case's body.

Next is the where clause. This is a boolean test, just like an if statement, so the case only matches if the expression is true. In this example, x.hasSuffix("pepper") looks at the bound value and checks to see if it ends with "pepper". The strings "red pepper", "green pepper", and "spicy hot pepper" would all match, but "peppercorns" would not.

default: 

In Swift, switch statements need to exhaust all the possible values of the matched value, so they typically end with a default: case block to handle any value that wasn't previously matched. The only time you wouldn't see this is if you're matching an enum with only a few values, since you could manually exhaust all the options.

like image 76
Nate Cook Avatar answered Sep 20 '22 11:09

Nate Cook