-
enum SomeEnumeration {
-
// enumeration definition goes here
-
}
-
-
-
-
enum CompassPoint {
-
case North
-
case South
-
case East
-
case West
- }
-
enum Planet {
-
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
- }
-
var directionToHead = CompassPoint.West
- directionToHead = .East
-
directionToHead = .South
-
switch directionToHead {
-
case .North:
-
print("Lots of planets have a north")
-
case .South:
-
print("Watch out for penguins")
-
case .East:
-
print("Where the sun rises")
-
case .West:
-
print("Where the skies are blue")
-
}
- // prints "Watch out for penguins"
-
et somePlanet = Planet.Earth
-
switch somePlanet {
-
case .Earth:
-
print("Mostly harmless")
-
default:
-
print("Not a safe place for humans")
-
}
- // prints "Mostly harmless"
-
enum Barcode {
-
case UPCA(Int, Int, Int, Int)
-
case QRCode(String)
-
}
-
-
var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)
-
productBarcode = .QRCode("ABCDEFGHIJKLMNOP")
-
-
-
switch productBarcode {
-
case .UPCA(let numberSystem, let manufacturer, let product, let check):
-
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
-
case .QRCode(let productCode):
-
print("QR code: \(productCode).")
-
}
-
// prints "QR code: ABCDEFGHIJKLMNOP."
-
-
-
-
switch productBarcode {
-
case let .UPCA(numberSystem, manufacturer, product, check):
-
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
-
case let .QRCode(productCode):
-
print("QR code: \(productCode).")
-
}
- // prints "QR code: ABCDEFGHIJKLMNOP."
-
enum ASCIIControlCharacter: Character {
-
case Tab = "\t"
-
case LineFeed = "\n"
-
case CarriageReturn = "\r"
- }
-
enum Planet: Int {
-
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
-
}
-
-
-
-
enum CompassPoint: String {
-
case North, South, East, West
-
}
-
-
-
-
let earthsOrder = Planet.Earth.rawValue
-
// earthsOrder is 3
-
-
let sunsetDirection = CompassPoint.West.rawValue
-
// sunsetDirection is "West"
-
-
-
-
let possiblePlanet = Planet(rawValue: 7)
-
// possiblePlanet is of type Planet? and equals Planet.Uranus
-
-
-
-
let positionToFind = 9
-
if let somePlanet = Planet(rawValue: positionToFind) {
-
switch somePlanet {
-
case .Earth:
-
print("Mostly harmless")
-
default:
-
print("Not a safe place for humans")
-
}
-
} else {
-
print("There isn't a planet at position \(positionToFind)")
-
}
- // prints "There isn't a planet at position 9"
-
enum ArithmeticExpression {
-
case Number(Int)
-
indirect case Addition(ArithmeticExpression, ArithmeticExpression)
-
indirect case Multiplication(ArithmeticExpression, ArithmeticExpression)
-
}
-
-
-
-
indirect enum ArithmeticExpression {
-
case Number(Int)
-
case Addition(ArithmeticExpression, ArithmeticExpression)
-
case Multiplication(ArithmeticExpression, ArithmeticExpression)
-
}
-
-
-
-
func evaluate(expression: ArithmeticExpression) -> Int {
-
switch expression {
-
case .Number(let value):
-
return value
-
case .Addition(let left, let right):
-
return evaluate(left) + evaluate(right)
-
case .Multiplication(let left, let right):
-
return evaluate(left) * evaluate(right)
-
}
-
}
-
-
// evaluate (5 + 4) * 2
-
let five = ArithmeticExpression.Number(5)
-
let four = ArithmeticExpression.Number(4)
-
let sum = ArithmeticExpression.Addition(five, four)
-
let product = ArithmeticExpression.Multiplication(sum, ArithmeticExpression.Number(2))
-
print(evaluate(product))
- // prints "18"