Skip to content

Instantly share code, notes, and snippets.

@alexj70
Created April 22, 2017 19:52
Show Gist options
  • Save alexj70/160b1693d09fb0e896cc7ac578dd738c to your computer and use it in GitHub Desktop.
Save alexj70/160b1693d09fb0e896cc7ac578dd738c to your computer and use it in GitHub Desktop.
if case let, if case let where
//////////////////////////////////
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 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 Media.movie(_, _, year) = m, year < 1888 {
print("Something seems wrong: the movie's year is before the first movie ever made.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment