Declaring
enum Direction {
case Right
case Left
}
// Or
enum Direction {
case Right, Left
}
// Or
enum Direction: String {
case Right = "Right turn only"
case Left = "Left turn only"
}
Using
var turn = Direction.Right
if (turn == Direction.Right) {
print("Turn \(turn)") // "Turn Right"
}
turn = .Left // Shorthand assignment
print("Turn \(turn)") // "Turn Left"
switch turn {
case .Right:
print("You will be turning right.")
case .Left:
print("You will be turning left.")
}
There is more to Enums than just this but to be honest, I will probably never use an Enum beyond this.
(Swift 2.2)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.