《swift2.0 官方教程中文版》 第3章-05模式


import Foundation


/*通配符模式********************************************/

//通配符模式由一个下划线(_)构成,且匹配并忽略任何值。当你不在乎被匹配的值时可以使用该模式。例如, 面这段代码在闭区间 1...3 中循环,每次循环时忽略该区间内的当前值:

for _ in 1...3 {

    // Do something three times.

}




/*标识符模式********************************************/

//标识符模式匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量声明中, someValue 是一个标识符模式,匹配了类型是 Int 42

let someValue = 42

//当匹配成功时, 42 被绑定(赋值)给常量 someValue




/*值绑定模式********************************************/

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

let point = (3,2)

switch point {

    case let(x,y):

        print("The point is at (\(x), \(y)).")

}

// prints "The point is at (3, 2).”




/*元组模式********************************************/

//元组模式是逗号分隔的,有零个或多个模式的列表,并被一对圆括号括起来。元组模式匹配相应元组类型的值。

let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]

// This code isn't valid.

//比如下面这段代码就不正确,因为 (x, 0) 中的元素 0 是一个表达式模式:

//for (x, 0) in points {

//    /* ... */

//}

//对于只包含一个元素的元组,括号是不起作用的。模式只匹配这个单个元素的类型。下面3条语句是等效的:

let a = 2 // a: Int = 2 

let (a2) = 2 // a: Int = 2

let (a3): Int = 2 // a: Int = 2




/*枚举用例模式********************************************/

//一个枚举用例模式匹配现有的某个枚举类型的某个用例(case)。枚举用例模式出现在 switch 语句中的case标签中,以及 if , while , guard for-in 语句的case条件中。




/*可选模式********************************************/

//可选模式与封装在一个 Optional(T) 或者一个 ExplicitlyUnwrappedOptional(T) 枚举中的 Some(T) 用例相匹配。可选模式由一个标识符模式和紧随其后的一个问号组成,在某些情况下表现为枚举用例模式。

let someOptional: Int? = 42

// Match using an enumeration case pattern

if case .Some(let x) = someOptional {

    print(x)

}

// Match using an optional pattern

if case let x? = someOptional {

    print(x)

}


//如果一个数组的元素是可选类型,可选模式为 for-in 语句提供了一种在该数组中迭代的简便方式,只为数组中的 非空 non-nil 元素执行循环体。

let arrayOfOptionalInts: [Int?] = [nil, 2, 3, nil, 5]

// Match only non-nil values

for case let number? in arrayOfOptionalInts {

    print("Found a \(number)")

}

//Found a 2 

//Found a 3 

//Found a 5




/*类型转换模式********************************************/

//有两种类型转换模式, is 模式和 as 模式。这两种模式只出现在 switch 语句中的case标签中。 is 模式和 as 模式有以下形式:

//is 模式仅当一个值的类型在运行时(runtime) is 模式右边的指定类型一致 - 或者是该类型的子类 - 的情况 ,才会匹配这个值。 is 模式和 is 操作符有相似表现,它们都进行类型转换,却舍弃返回的类型。

//as 模式仅当一个值的类型在运行时(runtime) as 模式右边的指定类型一致 - 或者是该类型的子类 - 的情 况下,才会匹配这个值。如果匹配成功,被匹配的值的类型被转换成 as 模式左边指定的模式。




/*表达式模式********************************************/

//一个表达式模式代表了一个表达式的值。表达式模式只出现在 switch 语句中的 case 标签中。

let point2 = (1,2)

switch point2 {

case (0,0):

    print("(0, 0) is at the origin.")

case (-2...2, -2...2):

    print("(\(point2.0), \(point2.1)) is near the origin.")

default:

    print("The point is at (\(point2.0), \(point2.1)).")


}

// prints "(1, 2) is near the origin.”


//你可以重载 ~= 操作符来提供自定义的表达式匹配行为。比如你可以重写上面的例子, point 表达式去比较字 符串形式的点。

// Overload the ~= operator to match a string with an integer 

func ~=(pattern: String, value: Int) -> Bool {

    return pattern == "\(value)"

}

switch point2 {

    case ("0", "0"):

    print("(0, 0) is at the origin.")

    case (-2...2, -2...2):

    print("(\(point2.0), \(point2.1)) is near the origin.")

    default:

    print("The point is at (\(point2.0), \(point2.1)).")

}

// prints "(1, 2) is near the origin.”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值