Enumerations
This page is under construction. Please come back later.
#!/usr/bin/env swift; import Foundation import Utils enum CompassPoint : Int { case NORTH case EAST case SOUTH case WEST } func CompassPointToString(_ c:CompassPoint) -> String { var result:String = "" switch c { case CompassPoint.NORTH: result = "North" case CompassPoint.EAST: result = "East" case CompassPoint.SOUTH: result = "South" case CompassPoint.WEST: result = "West" } return result } func turnRight(_ c:CompassPoint) -> CompassPoint { if c == CompassPoint.NORTH { return CompassPoint.EAST } else if c == CompassPoint.EAST { return CompassPoint.SOUTH } else if c == CompassPoint.SOUTH { return CompassPoint.WEST } else { return CompassPoint.NORTH } } // Begin Main var cp1:CompassPoint = CompassPoint.NORTH print("cp1: " + String(describing: cp1)) print("SOUTH: " + CompassPointToString(CompassPoint.SOUTH)) print("turnRight(cp1): " + CompassPointToString(turnRight(cp1))) var cp2:CompassPoint = CompassPoint.EAST if cp1 == cp2 { print("cp1 == cp2") } else { print("cp1 != cp2") } cp1 = cp2 if cp1 == cp2 { print("cp1 == cp2") } else { print("cp1 != cp2") } exit(EXIT_SUCCESS)
Output
$ swiftc Enums1.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#!/usr/bin/env swift; import Foundation import Utils enum Color : Int { case RED = 1 case GREEN = 2 case YELLOW = 3 case BLUE = 4 case MAGENTA = 5 case CYAN = 6 } func combineColors(_ c1:Color, _ c2:Color) -> Color { let i1:Int = c1.rawValue let i2:Int = c2.rawValue let result:Int = i1 | i2 let returnColor:Color? = Color(rawValue: result) return returnColor! } // Begin Main var c1:Color = Color.BLUE print("c1: " + String(describing: c1)) print("RED: " + String(describing: Color.RED)) print("combineColors(c1, RED): " + String(describing: combineColors(c1, Color.RED))) var c2:Color = Color.GREEN print("combineColors(c1, c2): " + String(describing: combineColors(c1, c2))) if c1 == c2 { print("c1 == c2") } else { print("c1 != c2") } c1 = c2 if c1 == c2 { print("c1 == c2") } else { print("c1 != c2") } var bad:Color? = Color(rawValue: 7) print("bad: " + String(describing: bad)) exit(EXIT_SUCCESS)
Output
$ swiftc Enums2.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]