美文网首页
模式匹配

模式匹配

作者: merlinCry | 来源:发表于2018-08-01 16:12 被阅读0次

    swift中的switch语句意思是匹配相同的模式,而不仅仅是相等(相等只是一种特例),并且还可以进行值绑定

    一 常见用法
    1. 最简单的相等匹配
    let a = 1
    switch a {
    case 1:
      print("1")
    default:
      print("not 1")
    }
    
    2. 枚举匹配,顺便绑定变量
    enum School {
      case Student(name: String, age: Int, address: String?)
      case Teacher(name: String, age: Int, address: String?)
    }
    let karl =  School.Student(name: "karl", age: 27, address: "AH")
    
    switch karl {
    //如果karl是Student顺便把他的关联值绑定给这三个常量
    case let .Student(name, age, address):
      print("\(name) -- \(age) -- \(address)")
    default:
      break
    }
    
    3. 元组匹配,通配符,where语句
    let point = CGPoint(x: 7, y: 0)
    switch (point.x, point.y) {
      case (0,0): print("On the origin!")
    //通配符表示不关心这个值,只要前面那个值等于0就可以了
      case (0,_): print("x=0: on Y-axis!")
      case (_,0): print("y=0: on X-axis!")
      case (let x, let y) where x == y: print("On y=x")
      default: print("Quite a random point here.")
    }
    

    其中第四个case中使用了where语句添加匹配条件。但是前面这个(let x, let y) 似乎仅仅是值绑定

    4.范围匹配

    可以判断值属于哪个范围

    let count = 7
    switch count {
      case Int.min..<0: print("Negative count, really?")
      case 0: print("Nothing")
      case 1: print("One")
      case 2..<5: print("A few")
      case 5..<10: print("Some")
      default: print("Many")
    }
    
    二 自定义模式匹配

    switch是如何判断模式是否匹配的呢?,实际上用的是 ~= 运算符
    如:

    let a = 1
    switch a {
    //这个case相当于  1 ~= a
    case 1:
      print("1")
    default:
      print("not 1")
    }
    

    //这样我们就可以通过重载 ~=运算符来为一些自定义类型添加模式匹配能力
    下面为自定义类型Affine和Int之间添加模式匹配

    struct Affine {
      var a: Int
      var b: Int
    }
    
    func ~= (lhs: Affine, rhs: Int) -> Bool {
      return rhs % lhs.a == lhs.b
    }
    
    switch 5 {
    //相当于  5 ~= Affine(a: 2, b: 0)
    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")
    }
    
    三 if case let,guard case let,for case let

    目前这几种写法用于枚举的时候比较好理解,用在其他类型还没搞明白
    case表示模式匹配,let表示只绑定,case let 就是如果模式匹配,那么将一些值绑定到let声明的常量。

    先看if case let
    enum Media {
      case Book(title: String, author: String, year: Int)
      case Movie(title: String, director: String, year: Int)
      case WebSite(urlString: String)
    }
    let m = Media.Movie(title: "Captain America: Civil War", director: "Russo Brothers", year: 2016)
    
    if case let Media.Movie(title, _, _) = m {
      print("This is a movie named \(title)")
    }
    //还可以追加判断条件year < 1888
    if case let Media.Movie(_, _, year) = m, year < 1888 {
      print("Something seems wrong: the movie's year is before the first movie ever made.")
    }
    
    二 guard case let 和if case let 的区别就是if 和 guard的区别
    三 for case let

    这个相当于在for语句要遍历的集合中添加筛选条件,只有模式匹配的才可以参加循环

    enum Media {
        case Book(title: String, author: String, year: Int)
        case Movie(title: String, director: String, year: Int)
        case WebSite(urlString: String)
    }
    
    let mediaList: [Media] = [
        .Book(title: "Harry Potter and the Philosopher's Stone", author: "J.K. Rowling", year: 1997),
        .Movie(title: "Harry Potter and the Philosopher's Stone", director: "Chris Columbus", year: 2001),
        .Book(title: "Harry Potter and the Chamber of Secrets", author: "J.K. Rowling", year: 1999),
        .Movie(title: "Harry Potter and the Chamber of Secrets", director: "Chris Columbus", year: 2002),
        .Book(title: "Harry Potter and the Prisoner of Azkaban", author: "J.K. Rowling", year: 1999),
        .Movie(title: "Harry Potter and the Prisoner of Azkaban", director: "Alfonso Cuarón", year: 2004),
        .Movie(title: "J.K. Rowling: A Year in the Life", director: "James Runcie", year: 2007),
        .WebSite(urlString: "https://en.wikipedia.org/wiki/List_of_Harry_Potter-related_topics")
    ]
    
    //遍历出所有movie
    for case let Media.Movie(title, _, year) in mediaList {
        print(" - \(title) (\(year))")
    }
    //也可以添加判断条件
    for case let Media.Movie(title, director, year) in mediaList, director == "Chris Columbus" {
      print(" - \(title) (\(year))")
    }
    
    

    相关文章

      网友评论

          本文标题:模式匹配

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