Skip to content

Instantly share code, notes, and snippets.

@EJSohn
Created May 2, 2018 11:45
Show Gist options
  • Save EJSohn/be48f34d8f403f80cb25afcd8a2a56fb to your computer and use it in GitHub Desktop.
Save EJSohn/be48f34d8f403f80cb25afcd8a2a56fb to your computer and use it in GitHub Desktop.
var directionToHead = CompassPoint.west
// or
var directionToHead: CompassPoint = .west
switch directionToHead {
case .west:
print("do something")
case .north:
print("do another thing")
default:
break
}
// Enumeration of type Fruit.
enum Fruit: Int {
case apple = 1000
case banana = 500
case grape = 2500
}
// declare variable.
let orderedFruit: Fruit = .apple
switch orderedFruit {
default:
print("\(orderedFruit.rawValue)원 주세요!")
}
enum Cloud {
case cirrus
case cumulus
case altocumulus
case cumulonimbus
}
enum WeatherCondition {
case sunny(temperature: Float)
case rainy(inchesPerHour: Float)
case cloudy(cloudType: Cloud, windSpeed: Float)
}
// declare variables.
var today = WeatherCondition.sunny(24)
var yesterday = WeatherCondition.cloudy(Cloud.cirrus, 10)
switch today {
case .sunny(let temperature):
print("today's temperature is \(temperature).")
case .rainy(let inchesPerHour):
print("\(inchesPerHour) inches of rainwater.")
case .cloudy(let cloudType, let windSpeed):
switch cloudType {
case .cirrus:
print("it's cloudy cirrus.")
default:
print("somethig blah..")
}
print("wind speed: \(windSpeed)")
}
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
public enum Result<Value> {
case Success(Value)
case Failure(NSData?, ErrorType)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment