Created
April 22, 2017 19:52
-
-
Save alexj70/160b1693d09fb0e896cc7ac578dd738c to your computer and use it in GitHub Desktop.
if case let, if case let where
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////// | |
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