发布于 2015-06-14 02:01:58 | 373 次阅读 | 评论: 0 | 来源: 网络整理

值绑定模式(Value-Binding Pattern)

值绑定模式绑定匹配的值到一个变量或常量。当绑定匹配值给常量时,用关键字let,绑定给变量时,用关键之var

标识符模式包含在值绑定模式中,绑定新的变量或常量到匹配的值。例如,你可以分解一个元组的元素,并把每个元素绑定到相应的标识符模式中。


let point = (3, 2)
switch point {
    // Bind x and y to the elements of point.
case let (x, y):
    println("The point is at ((x), (y)).")
}
// prints "The point is at (3, 2).”

在上面这个例子中,let将元组模式(x, y)分配到各个标识符模式。因为这种行为,switch语句中case let (x, y):case (let x, let y):匹配的值是一样的。


值绑定(Value Binding)模式语法
值绑定模式 → var 模式 | let 模式
 

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务