Skip to content

Instantly share code, notes, and snippets.

@SatoTakeshiX
Forked from akio0911/CodePiece.swift
Created September 18, 2016 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SatoTakeshiX/bb1011e847393dca945b8ea0cbbcb731 to your computer and use it in GitHub Desktop.
Save SatoTakeshiX/bb1011e847393dca945b8ea0cbbcb731 to your computer and use it in GitHub Desktop.
パターンマッチの例 #swift2 #CodePiece #swift #swiftlang
enum Gender {
case Male, Female
}
enum TimeSaleResult {
case Normal, Morning, Evening
}
func TimeSale(hour:Int) -> TimeSaleResult {
switch hour {
case let h where h < 10:
return .Morning
case let h where 20 < h:
return .Evening
default:
return .Normal
}
}
let gender = Gender.Female
let age = 30
let hour = 12
switch (gender, age, hour) {
case (.Female, _ , _ ): print("女性割引")
case (_ , let age, _ ) where age < 20: print("学生")
case (_ , let age, _ ) where 60 < age: print("シニア")
case (_ , _ , hour) where hour < 10: print("早朝割引")
case (_ , _ , hour) where 19 < hour: print("深夜割引")
default: print("通常割引")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment