
let x where x.hasSuffix("pepper") 这是什么意思?
注意:没有必要使用?这让我很困惑..这是足够的x哪里x.hasSuffix(“胡椒”)?因为,让x应该已经被分配了.
更新:
从@Jacky在这里的评论,可能意义如下.
let x = vegetable if (x.hasSuffix("pepper") ......解决方法 在该上下文中使用的位置为 pattern matching.从示例中: case let x where x.hasSuffix("pepper"): 当x的后缀匹配“胡椒”时,它将设置常量vegetableComment:
let vegetableComment = "Is it a spicy \(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."
因为这些情况是在case之前让x,其中x.hasSuffix(“pepper”):.您可以尝试改变它们的顺序并传递值“芹菜”,看看不同的结果.
编辑:
从我的理解,如果x的后缀是“胡椒”,它将创建一个常量x.创建此常数的目标是让您在之后使用它:
let vegetableComment = "Is it a spicy \(x)?"
编辑2:
经过一番研究,这被称为价值绑定,它被描述为:
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.
摘录自:苹果公司“Swift编程语言”iBooks. https://itun.es/gb/jEUH0.l
总结以上是内存溢出为你收集整理的ios – “where”的含义是什么?全部内容,希望文章能够帮你解决ios – “where”的含义是什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)