“where”关键字是什么意思?

时间:2022-07-01 01:50:05

I couldn't understand exact meaning of this statement.

我无法理解这句话的确切含义。

let x where x.hasSuffix("pepper")

What is meaning of that?

这是什么意思?

Note: There is no need of let use? It makes me confusing.. Is this enough x where x.hasSuffix("pepper")? because, let x should be already get assigned.?

注意:没有必要让我们使用?这让我感到困惑..这是否足够x x.hasSuffix(“胡椒”)?因为,我们应该已经分配了x。

Update: From @Jacky comment here, it could be meaning same as below.

更新:来自@Jacky在这里评论,它可能与下面的含义相同。

let x = vegetable 
if (x.hasSuffix("pepper")
 ......

4 个解决方案

#1


18  

The where in that context is used as pattern matching. From the example:

该上下文中的位置用作模式匹配。从示例:

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

When the suffix of x matches "pepper" it will set the constant vegetableComment:

当x的后缀与“pepper”匹配时,它将设置常量vegetableComment:

let vegetableComment = "Is it a spicy \(x)?"

You can see as well that x can´t be "celery", "cucumber" or "watercress", otherwise it would give you a different outcome:

你也可以看到x不能是“芹菜”,“黄瓜”或“水田芥”,否则它会给你一个不同的结果:

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."

Because those cases are before case let x where x.hasSuffix("pepper"):. You can try changing the order of them and pass the value "celery" to see a different outcome.

因为那些案例是在案件之前让x在哪里x.hasSuffix(“胡椒”):您可以尝试更改它们的顺序并传递值“celery”以查看不同的结果。

Edit:

编辑:

From my understanding it creates a constant x if x's suffix is "pepper". The goal of creating this constant, is for you to use it after that:

根据我的理解,如果x的后缀是“pepper”,它会创建一个常量x。创建此常量的目的是让您在此之后使用它:

let vegetableComment = "Is it a spicy \(x)?"

Edit 2:

编辑2:

After a bit more research, that's called value binding and it's described as:

经过一番研究,这称为价值绑定,它被描述为:

switch case can bind the value or values it matches to temporary constants or variables, for use in the body of the case. This is known as value binding, because the values are “bound” to temporary constants or variables within the case’s body.

switch case可以将它匹配的值绑定到临时常量或变量,以便在case的主体中使用。这称为值绑定,因为值“绑定”到案例正文中的临时常量或变量。

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

摘录自:Apple Inc.“The Swift Programming Language。”iBooks。 https://itun.es/gb/jEUH0.l

#2


7  

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

The simple explanation is that you cannot match a case, that is of type String, with .hasSuffix() because it returns a Bool. So, they give you this where pattern matching keyword to use. It works like this:

简单的解释是你不能将一个String类型的case与.hasSuffix()匹配,因为它返回一个Bool。所以,他们给你这个模式匹配关键字使用的地方。它的工作原理如下:

  1. let x copies the String value you are passing into the switch to the constant x.
  2. 让x将传递给开关的String值复制到常量x。
  3. where is a boolean evaluation that will only let the case complete the match if it is given a true bool, just like an if block.
  4. 其中是一个布尔值的评估,如果给出一个真正的bool,就会让case完成匹配,就像if块一样。
  5. hasSuffix() returns the bool required by where.
  6. hasSuffix()返回where所需的bool。

If your string variable passed into the switch is var foo. You can literally do this instead:

如果传入交换机的字符串变量是var foo。你可以改为:

case foo where foo.hasSuffix("pepper")

You can pass a true bool to where like this and it would work, uselessly:

你可以把一个真正的bool传递到这样的地方,它会工作,无用:

case foo where true

#3


4  

This is a larger view of the code:

这是一个更大的代码视图:

switch vegetable {

... omissis ...

case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(x)?"
default:
    let vegetableComment = "Everything tastes good in soup."
}

What it does is match the value of vegetable assigning it to x and testing if it has suffix "pepper". If the match succeeds it executes the case block let vegetableComment = "Is it a spicy \(x)?" otherwise it continues with the next test (which in this case is a default:).

它的作用是将蔬菜的值分配给x并测试它是否有后缀“pepper”。如果匹配成功,则执行case block let vegetableComment =“这是辣吗(x)?”否则它继续下一个测试(在这种情况下是默认:)。

Note that the let is necessary. It means that you are binding a new variable x.

请注意,let是必要的。这意味着您绑定了一个新变量x。

Also note that it's not the same as

还要注意它与它不一样

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

as this always succeeds and enters the case block, while using the where clause means that if the condition is not satisfied the match fails and the next case (or default) shall be tried.

因为这总是成功并进入case块,而使用where子句意味着如果不满足条件,则匹配失败并且将尝试下一个case(或default)。

#4


-1  

"where" clause introduced in swift 2 but never got a recognition from developers for its usage. It is used for pattern matching and can be used with for-in, switch statements. It is basically a part of control flow and can be used anywhere in your code. Some examples are as follows

在swift 2中引入了“where”子句,但从未得到开发人员对其使用的认可。它用于模式匹配,可以与for-in,switch语句一起使用。它基本上是控制流的一部分,可以在代码中的任何位置使用。一些例子如下

//Example usage in switch
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary point")
}

//Example usage in for
let arr = [1,2,3,4]
for value in arr where value != 0 {
    print(value)
}

#1


18  

The where in that context is used as pattern matching. From the example:

该上下文中的位置用作模式匹配。从示例:

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

When the suffix of x matches "pepper" it will set the constant vegetableComment:

当x的后缀与“pepper”匹配时,它将设置常量vegetableComment:

let vegetableComment = "Is it a spicy \(x)?"

You can see as well that x can´t be "celery", "cucumber" or "watercress", otherwise it would give you a different outcome:

你也可以看到x不能是“芹菜”,“黄瓜”或“水田芥”,否则它会给你一个不同的结果:

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."

Because those cases are before case let x where x.hasSuffix("pepper"):. You can try changing the order of them and pass the value "celery" to see a different outcome.

因为那些案例是在案件之前让x在哪里x.hasSuffix(“胡椒”):您可以尝试更改它们的顺序并传递值“celery”以查看不同的结果。

Edit:

编辑:

From my understanding it creates a constant x if x's suffix is "pepper". The goal of creating this constant, is for you to use it after that:

根据我的理解,如果x的后缀是“pepper”,它会创建一个常量x。创建此常量的目的是让您在此之后使用它:

let vegetableComment = "Is it a spicy \(x)?"

Edit 2:

编辑2:

After a bit more research, that's called value binding and it's described as:

经过一番研究,这称为价值绑定,它被描述为:

switch case can bind the value or values it matches to temporary constants or variables, for use in the body of the case. This is known as value binding, because the values are “bound” to temporary constants or variables within the case’s body.

switch case可以将它匹配的值绑定到临时常量或变量,以便在case的主体中使用。这称为值绑定,因为值“绑定”到案例正文中的临时常量或变量。

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

摘录自:Apple Inc.“The Swift Programming Language。”iBooks。 https://itun.es/gb/jEUH0.l

#2


7  

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

The simple explanation is that you cannot match a case, that is of type String, with .hasSuffix() because it returns a Bool. So, they give you this where pattern matching keyword to use. It works like this:

简单的解释是你不能将一个String类型的case与.hasSuffix()匹配,因为它返回一个Bool。所以,他们给你这个模式匹配关键字使用的地方。它的工作原理如下:

  1. let x copies the String value you are passing into the switch to the constant x.
  2. 让x将传递给开关的String值复制到常量x。
  3. where is a boolean evaluation that will only let the case complete the match if it is given a true bool, just like an if block.
  4. 其中是一个布尔值的评估,如果给出一个真正的bool,就会让case完成匹配,就像if块一样。
  5. hasSuffix() returns the bool required by where.
  6. hasSuffix()返回where所需的bool。

If your string variable passed into the switch is var foo. You can literally do this instead:

如果传入交换机的字符串变量是var foo。你可以改为:

case foo where foo.hasSuffix("pepper")

You can pass a true bool to where like this and it would work, uselessly:

你可以把一个真正的bool传递到这样的地方,它会工作,无用:

case foo where true

#3


4  

This is a larger view of the code:

这是一个更大的代码视图:

switch vegetable {

... omissis ...

case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(x)?"
default:
    let vegetableComment = "Everything tastes good in soup."
}

What it does is match the value of vegetable assigning it to x and testing if it has suffix "pepper". If the match succeeds it executes the case block let vegetableComment = "Is it a spicy \(x)?" otherwise it continues with the next test (which in this case is a default:).

它的作用是将蔬菜的值分配给x并测试它是否有后缀“pepper”。如果匹配成功,则执行case block let vegetableComment =“这是辣吗(x)?”否则它继续下一个测试(在这种情况下是默认:)。

Note that the let is necessary. It means that you are binding a new variable x.

请注意,let是必要的。这意味着您绑定了一个新变量x。

Also note that it's not the same as

还要注意它与它不一样

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

as this always succeeds and enters the case block, while using the where clause means that if the condition is not satisfied the match fails and the next case (or default) shall be tried.

因为这总是成功并进入case块,而使用where子句意味着如果不满足条件,则匹配失败并且将尝试下一个case(或default)。

#4


-1  

"where" clause introduced in swift 2 but never got a recognition from developers for its usage. It is used for pattern matching and can be used with for-in, switch statements. It is basically a part of control flow and can be used anywhere in your code. Some examples are as follows

在swift 2中引入了“where”子句,但从未得到开发人员对其使用的认可。它用于模式匹配,可以与for-in,switch语句一起使用。它基本上是控制流的一部分,可以在代码中的任何位置使用。一些例子如下

//Example usage in switch
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary point")
}

//Example usage in for
let arr = [1,2,3,4]
for value in arr where value != 0 {
    print(value)
}