Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift常数赋值示例 我是一个长时间的开发者(java,C++,C++等),一个带问题的新手。在下面定义常量“x”的switch语句中,示例陈述了“注意如何在模式中使用let将与模式匹配的值指定给常量。”。我有点困惑,因为该语句根本没有引用switch语句“vegeture”的目标变量。我的想法是它应该是: case let x where vegetable.hasSuffix("pepper"): -- or just -- case let x where hasSuffix("pepper"):_Ios_Swift_Constants - Fatal编程技术网

Ios Swift常数赋值示例 我是一个长时间的开发者(java,C++,C++等),一个带问题的新手。在下面定义常量“x”的switch语句中,示例陈述了“注意如何在模式中使用let将与模式匹配的值指定给常量。”。我有点困惑,因为该语句根本没有引用switch语句“vegeture”的目标变量。我的想法是它应该是: case let x where vegetable.hasSuffix("pepper"): -- or just -- case let x where hasSuffix("pepper"):

Ios Swift常数赋值示例 我是一个长时间的开发者(java,C++,C++等),一个带问题的新手。在下面定义常量“x”的switch语句中,示例陈述了“注意如何在模式中使用let将与模式匹配的值指定给常量。”。我有点困惑,因为该语句根本没有引用switch语句“vegeture”的目标变量。我的想法是它应该是: case let x where vegetable.hasSuffix("pepper"): -- or just -- case let x where hasSuffix("pepper"):,ios,swift,constants,Ios,Swift,Constants,而不是 case let x where x.hasSuffix("pepper"): 任何反馈都将不胜感激 其中的谓词可以直接引用蔬菜,这是一种特殊情况,它编译得很好 switch vegetable { case "celery": print("Add some raisins and make ants on a log.") case "cucumber", "watercress": print("That would make a good tea sa

而不是

    case let x where x.hasSuffix("pepper"):
任何反馈都将不胜感激


其中的
谓词可以直接引用
蔬菜
,这是一种特殊情况,它编译得很好

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

但是现在考虑一个更复杂的模式匹配<代码>蔬菜<代码>,假设它是一个<代码>字符串?< /代码>(A.K.A.代码>可选< /COD>),而不是<代码>字符串< /代码>:

let vegetable: String? = "red pepper"
switch vegetable {
case "celery"?:
    print("Add some raisins and make ants on a log.")
case "cucumber"?, "watercress"?:
    print("That would make a good tea sandwich.")
case let x? where vegetable.hasSuffix("pepper"):
    //            ^ error: value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?
    print("Is it a spicy \(x)?")
case nil:
    print("You don't even have a vegetable!")
default:
    print("Everything tastes good in soup.")
}

在这种情况下,
x
是非零情况下的
蔬菜的非可选值,允许用户在其上调用
.hasSuffix(“胡椒”)
。它的意思是“如果蔬菜有一个值,称之为
x
,恰好以
“pepper”
,然后这样做:…”

在与@Alexander聊天后,我对开关结构有了更好的理解。感谢他的时间和解释

正如本文档“值绑定”一节中详细介绍的,我了解到switch语句在评估案例之前将临时变量分配给switch目标的值。因此,将开关目标值“red pepper”分配给右侧(临时x),并从上到下匹配每个开关盒的盒模式。如果大小写匹配,则左侧x将成为永久常量。Net,定义临时x并设置=然后执行开关模式匹配

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

谢谢你的回复,亚历山大。nil的情况很有趣,但我不清楚原始示例中使用“x.hasSuffix()”的地方。何时或何地为x分配了蔬菜的开关目标值以检查其是否包含后缀?
何时或何地为x分配了蔬菜的开关目标值
,这是模式匹配中的
let
语义的一部分。在这样一个非穷举切换中,每个模式都从上到下进行尝试,当以前的所有情况都不匹配时,就会触发这个模式。这很像是一个
if let x=…
语句。让我困惑的是“x.hasSuffix()”中的“x.”。根据我的经验,这句话说x是模式匹配的,而不是蔬菜。X被分配一个值,该值基于模式匹配,而该模式匹配的对象除了常数的Swift默认值外,从未被分配过值。我的期望是这个案子永远不会被触发。我会尝试代码,但我的开发环境还没有准备好。另外,据我所知,用Let定义的常量是不可变的,那么如何将常量定义为对常量本身进行操作的结果呢?
那句话说x将是模式匹配的,而不是植物。
->No,模式匹配的是蔬菜
x
是绑定到交换机目标(
蔬菜
)的临时名称,以便在案例的
where
子句中进行讨论。
 case let x where x.hasSuffix("pepper"):