Swift3.0-模式匹配总结

Swift模式匹配总结

模式代表单个值或者复合值的结构。例如,元组 (1, 2) 的结构是由逗号分隔的,包含两个元素的列表。因为模式代表一种值的结构,而不是特定的某个值,你可以利用模式来匹配各种各样的值。比如,(x, y)可以匹配元组 (1, 2),以及任何含两个元素的元组。除了利用模式匹配一个值以外,你可以从复合值中提取出部分或全部值,然后分别把各个部分的值和一个常量或变量绑定起来。

Swift 中的模式分为两类:一种能成功匹配任何类型的值,另一种在运行时匹配某个特定值时可能会失败。

第一类模式用于解构简单变量、常量和可选绑定中的值。此类模式包括通配符模式、标识符模式,以及包含前两种模式的值绑定模式和元组模式。你可以为这类模式指定一个类型标注,从而限制它们只能匹配某种特定类型的值。

第二类模式用于全模式匹配,这种情况下你试图匹配的值在运行时可能不存在。此类模式包括枚举用例模式、可选模式、表达式模式和类型转换模式。你在 switch 语句的 case 标签中,do 语句的 catch子句中,或者在 ifwhileguardfor-in 语句的 case 条件句中使用这类模式。

通配符模式(Wildcard Pattern)

如果你在 Swift 编码中使用了 _ 通配符,就可以表示你使用了通配符模式。 _ 用于匹配并忽略任何值。

例如,下面这段代码在闭区间 1...3 中迭代,每次迭代都忽略该区间的当前值:

for _ in 1...3 {
    // ...
}
标识符模式(Identifier Pattern)

定义变量或者常量时候,可以认为变量名和常量名就是一个标识符模式,用于接收和匹配一个特定类型的值:

let i = 21 // i 就是一个标识符模式
值绑定模式(Value-Binding Pattern)

值绑定在 if 语句和 switch 语句中用的较多。 比如 if letcase let, 还有可能是 case var。 let 和 var 分别对应绑定为常量和变量。

var str: String? = "test"
if let v = str {
        // use v to do something
}
元组模式(Tuple Pattern)

顾名思义,元组模式就是匹配元组中元素的模式:

let person = ("Helen", 25)
 switch person {
    case ("Helen", let age):
        print("Your name is Helen, and you are \(age)" + " years old")
    case (_, 13...19):
        print("You are a teenager")
    case ("Bob", _):
        print("You are not a teenager, but your name" + " is Bob.")
    case (_, _):
        print("no comment")
}

但是在 for-in 语句中,由于每次循环都需要明确匹配到具体的值,所以以下代码是错误的:

 let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
 // 下面的代码是错误的
for (x, 0) in points {
    /* ... */
}

需要使用一个另外一个值绑定模式,来达成以上的逻辑:

let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
for (x, y) in points {
    if(y == 0) {
        print(x)
    }
}
枚举用例模式(Enumeration Case Pattern)

枚举用例模式是功能最强大的一个模式,也是整个模式匹配当中,应用面最广的模式,结合枚举中的关联值语法,可以做很多事情。先看一个简单的例子:

enum CompassPoint {
    case North
    case South
    case East
    case West
}

let directionToHead = CompassPoint.South
switch directionToHead {
    case .North:
        print("Lots of planets have a north")
    case .South:
        print("Watch out for penguins")
    case .East:
        print("Where the sun rises")
    case .West:
        print("Where the skies are blue")
}
// 输出 "Watch out for penguins”
可选模式

语句 case let x = y 模式允许你检查 y 是否能匹配 x

if case let x = y { … } 严格等同于 switch y { case let x: … }:当你只想与一条 case 匹配时,这种更紧凑的语法尤其有用。有多个 case 时更适合使用 switch

可选模式就是包含可选变量定义模式,在 if case、 for case、 switch-case 会用到。注意 if case letif let的区别:

 // 使用可选模式匹配
if case let x? = someOptional {
    print(x)
}
let arrayOfOptionalInts: [Int?] = [nil, 2, 3, nil, 5]
// 只匹配非 nil 的元素
for case let number? in arrayOfOptionalInts {
    print("Found a \(number)")
}
// Found a 2
// Found a 3
// Found a 5
类型转换模式(Type-Casting Pattern)

使用 isas 关键字的模式,就叫类型转换模式:

let i: Any = 1

switch i {
    case let i as Int:
        print("is a int")
    case let i as String:
        print("is a string")
    default:
        print("is another type")
}

switch i {
case is Int:
    print("is a int")
case is String:
    print("is a string")
default:
    print("is another type")
}
表达式模式

表达式模式只出现在 switch-case 中,Swift的模式匹配是基于~=操作符的,如果表达式的~=值返回true则匹配成功。可以自定义~=运算符,自定义类型的表达式匹配行为:

struct Affine {
  var a: Int
  var b: Int
}

func ~= (lhs: Affine, rhs: Int) -> Bool {
  return rhs % lhs.a == lhs.b
}

switch 5 {
case Affine(a: 2, b: 0): print("Even number")
case Affine(a: 3, b: 1): print("3x+1")
case Affine(a: 3, b: 2): print("3x+2")
default: print("Other")
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • 本章将会介绍 控制流For-In 循环While 循环If 条件语句Switch 语句控制转移语句 continu...
    寒桥阅读 4,030评论 0 0
  • 青龙湖畔花事了,群芳渐次少。 水平天阔望云闲,妙音幽幽耳畔唱流年。 一池落花如浮藻,昨日风非小。 来来往往匆匆人,...
    windbyside阅读 2,795评论 2 3
  • 幽幽月色 滚滚红尘 谈不尽相思几许 章台阁下 红杏枝头 念不清情字奈何 菁华园里 理不清春色 象牙塔中 剪不断韶光...
    999x48阅读 2,924评论 0 0
  • [TOC] 1、LLVM是什么? LLVM(Low Level Virtual Machine)是一个底层虚拟机,...
    Pandakingli阅读 9,677评论 0 0