模式匹配第四弹:if case,guard case,for case

做者:Olivier Halligon,原文连接,原文日期:2016-05-16
译者:walkingway;校对:Cee;定稿:numbbbbbgit

如今咱们来从新回顾下前三弹模式匹配的各类语法 第一弹第二弹第三弹,第四弹是本系列的最后一篇文章,本章会教你们使用 if case letfor case where 等一些高级语法,让咱们拭目以待吧!github

本篇文章会结合本系列前三篇文章提到的语法,而后将它们应用在一些更先进表达式中。swift

这篇文章是模式匹配系列文章的最后一部分,你能够在这里阅读全部内容:第一弹第二弹第三弹第四弹设计模式

if case let

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

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

例如,咱们有一个与以前文章相似的枚举数组:函数

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)")
}

改用 switch 后更冗长的版本:this

switch m {
  case let Media.Movie(title, _, _):
    print("This is a movie named \(title)")
  default: () // do nothing, but this is mandatory as all switch in Swift must be exhaustive
}

if case let where

咱们固然还能够将 if case letwhere 从句组合在一块儿用:url

if case let Media.Movie(_, _, year) = m where year < 1888 {
  print("Something seems wrong: the movie's year is before the first movie ever made.")
}

这种方式能够组合成一个至关强大的表达式,而改用 switch 实现可能会变得很是复杂,须要写不少行代码来检测那一个特定的 case。

guard case let

固然,guard case let 相似于 if case let,你可使用 guard case letguard case let … where … 来确保匹配一个模式或一个条件,而当没法匹配模式或知足条件时就退出。

enum NetworkResponse {
  case Response(NSURLResponse, NSData)
  case Error(NSError)
}

func processRequestResponse(response: NetworkResponse) {
  guard case let .Response(urlResp, data) = response,
    let httpResp = urlResp as? NSHTTPURLResponse
    where 200..<300 ~= httpResp.statusCode else {
      print("Invalid response, can't process")
      return
  }
  print("Processing \(data.length) bytes…")
  /* … */
}

for case

forcase 组合在一块儿也能让你有条件地遍历一个集合对象。使用 for case … 语义上相似于 for 循环,并且将它整个循环体封装在了 if case 的结构之中:它只会遍历、处理那些模式匹配了的元素。

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")
]

print("Movies only:")
for case let Media.Movie(title, _, year) in mediaList {
  print(" - \(title) (\(year))")
}
  
/* Output:
Movies only:
  • Harry Potter and the Philosopher's Stone (2001)

  • Harry Potter and the Chamber of Secrets (2002)

  • Harry Potter and the Prisoner of Azkaban (2004)

  • J.K. Rowling: A Year in the Life (2007)

    */

for case where

for case 增长一个 where 从句,能使其变得更增强大:

print("Movies by C. Columbus only:")
for case let Media.Movie(title, director, year) in mediaList where director == "Chris Columbus" {
  print(" - \(title) (\(year))")
}

/* Output:
Movies by C. Columbus only:
  • Harry Potter and the Philosopher's Stone (2001)

  • Harry Potter and the Chamber of Secrets (2002)

    */

?注意:使用 for … where 而不带 case 模式匹配依然是符合 Swift 语法规则的。好比你这样写也是 OK 的:

for m in listOfMovies where m.year > 2000 { … }

这里没有使用模式匹配(没有 case 或 ~=),所以有点超出了本系列的主题范围,可是这种写法是彻底有效的,并且这种构造也很是有用---特别是避免了将一个巨大的判断逻辑 if 结构(或是一个 guard … else { continue })封装在 for 的循环体内。

将它们组合起来使用

如今咱们终于要迎来这系列文章的大结局了:把咱们以前所学从头至尾串联起来(包括一些咱们在以前章节学习到的相似 x? 这种语法糖):

extension Media {
  var title: String? {
    switch self {
    case let .Book(title, _, _): return title
    case let .Movie(title, _, _): return title
    default: return nil
    }
  }
  var kind: String {
    /* Remember part 1 where we said we can omit the `(…)` 
    associated values in the `case` if we don't care about any of them? */
    switch self {
    case .Book: return "Book"
    case .Movie: return "Movie"
    case .WebSite: return "Web Site"
    }
  }
}

print("All mediums with a title starting with 'Harry Potter'")
for case let (title?, kind) in mediaList.map({ ($0.title, $0.kind) })
  where title.hasPrefix("Harry Potter") {
    print(" - [\(kind)] \(title)")
}

上面的代码可能看上去有点复杂,咱们先拆分一下:

  • 使用 map 函数将 Array<Media> 类型的数组 mediaList 转换成一个包含元组 [(String?, String)] 的数组,而其中的元组包含两个元素:第一个是标题(String? 类型),第二个是元素的种类(String 类型)

  • 它只当 title? 匹配时整个表达式才会匹配──还记得第三弹的那个语法糖吗:「当 switch 处理一个可选值 x? 时,你能够识别问号标记的可选值」,所以这里的 title? 至关于 .Some(title),它是不会匹配 title 为 nil 的状况的(译者注:至于为何要写成 title? 上一弹也有说明:由于后面与之匹配的是一个可选值(mediaList.map(...) 的 title),匹配类型要一致,不然会报错。)──所以匹配的结果是剔除全部 $0.titlenilmedia(也就是 title 为 Optional.None)──最终剩下的 media 中不包括 WebSite 类型,由于它没有 title

  • 而后再进一步去遍历 media,判断他们的 title 是否知足 title.hasPrefix("Harry Potter") 条件

最后这段代码将遍历每个 medium,筛选出那些以 “Harry Potter” 开头的,在这一过程当中将丢弃那些没有标题的,好比 WebSite,还有那些标题不以 "Harry Potter" 开头的 medium,这也包括做者 J.K.罗琳的记录片。

最终的输出结果以下,只有和 Harry Potter 相关的书籍和电影:

All medium with a title starting with 'Harry Potter'
  • [Book] Harry Potter and the Philosopher's Stone

  • [Movie] Harry Potter and the Philosopher's Stone

  • [Book] Harry Potter and the Chamber of Secrets

  • [Movie] Harry Potter and the Chamber of Secrets

  • [Book] Harry Potter and the Prisoner of Azkaban

  • [Movie] Harry Potter and the Prisoner of Azkaban

若是不使用模式匹配、where 从句、或是前面提到的各类语法糖,代码写出来多是这样的:

print("All mediums with a title and starting with 'Harry Potter'")
for media in mediaList {
  guard let title = media.title else {
    continue
  }
  guard title.hasPrefix("Harry Potter") else {
    continue
  }
  print(" - [\(media.kind)] \(title)")
}

有些人可能以为这种写法可读性更好,但你没法否定 for case let (title?, kind) in … where … 确实强大,能够在小伙伴面前装个逼,并且能够将循环 + 模式匹配 + where 从句组合起来是使用 ✨。

结论

「模式匹配」系列文章到此就所有结束了,但愿你能喜欢它,并真正学到了一些有趣的东西 ?。

下一篇文章我将聚焦 Swift 的设计模式与架构,而再也不是语言自己的语法了。

? 若是关于 Swift 你有什么特别想要从我这里了解到的,请不要犹豫,直接在 Twitter 上联系我吧,个人下一篇文章的灵感极可能就来自于大家的建议。

模式匹配和参数的顺序颠倒会致使语法错误。为了方便记忆,想像一下 switchcase let Media.Movie(…) 的顺序,他们是一致的。这样你就知道应该是 if case let Media.Movie(…) = m ,而不是 if case let m = Media.Movie(…),后者是彻底不会编译的。和 switch 中所作的同样,将 case 和模式((Media.Movie(title, _, _))放在一块儿,而不是与变量(m)在一块儿。
本文由 SwiftGG 翻译组翻译,已经得到做者翻译受权,最新文章请访问 http://swift.gg

相关文章
相关标签/搜索