美文网首页Swift3.0基础语法
Swift3.0-模式匹配总结

Swift3.0-模式匹配总结

作者: 蛊毒_ | 来源:发表于2017-05-12 12:39 被阅读481次
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")
}

相关文章

  • Swift3.0-模式匹配总结

    Swift模式匹配总结 模式代表单个值或者复合值的结构。例如,元组 (1, 2) 的结构是由逗号分隔的,包含两个元...

  • ios 经典面试案例 (七)

    Swift有哪些模式匹配? 模式匹配总结: 1.通配符模式(Wildcard Pattern)如果你在 Swift...

  • Swift 模式匹配总结

    Swift 模式匹配总结 基本用法 对枚举的匹配: 在swift中 不需要使用break跳出当前匹配,默认只执行一...

  • 模式匹配

    模式匹配之字符串 模式匹配之匹配类型 模式匹配之匹配数组、元组、集合 模式匹配之样例类 模式匹配之偏函数

  • Swift3.0- 类型转换、Any、AnyObject(可选链

    Swift3.0- 类型转换、Any、AnyObject(可选链)

  • 字符串匹配算法总结

    字符串匹配算法总结 所有代码集合 在一个主串中匹配模式串 BF算法   最简单的使用strcmp逐个匹配的算法, ...

  • Spark中RDD、DataFrame和DataSet之间简单的

    总结: rdd -> df 调用toDf(String*)rdd -> ds 需要有类型,所以需要先模式匹配,然后...

  • Activity的生命周期和启动模式

    总结Activity的生命周期和启动模式 生命周期 启动模式 匹配规则(待补充) 生命周期 a. onCreate...

  • 多模式串匹配 - AC 自动机

    多模式串匹配概念 多模式串匹配,即多个模式串在一个主串中进行匹配。 虽然单模式串也能完成多模式串的匹配,但每个模式...

  • Scala模式匹配

    模式匹配match 模式匹配不仅可以匹配值(case 1)还可以匹配类型(case s:String) 匹配过程有...

网友评论

    本文标题:Swift3.0-模式匹配总结

    本文链接:https://www.haomeiwen.com/subject/mrbotxtx.html