Skip to content

Instantly share code, notes, and snippets.

@clarkeben
Last active January 26, 2023 14:46
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 clarkeben/866598d4aa41af8f174ce14b360cec46 to your computer and use it in GitHub Desktop.
Save clarkeben/866598d4aa41af8f174ce14b360cec46 to your computer and use it in GitHub Desktop.
Enumerations (enum) in Swift
// MARK: - Associated Values
enum FavouriteAppleProducts {
case iphone(String)
case macbookPro(String)
case iPod(String)
}
let myFavouriteIpod = FavouriteAppleProducts.iphone("iPhone 14 pro")
// MARK: - CaseIterable
enum Fruits: CaseIterable {
case orange, apple, pear, mango
}
for fruit in Fruits.allCases {
print("I like to eat \(fruit)")
}
// MARK: - Example Enum
enum Compass {
case north
case east
case south
case west
}
enum Colours: String {
case red = "red"
case blue = "blue"
case green = "green"
case orange = "orange"
case purple = "purple"
}
// MARK: - Switch statement & Enum
var playerMove = Compass.north
switch playerMove {
case .north: print("Played moved north")
case .east: print("Played moved north")
case .south: print("Played moved north")
case .west: print("Played moved north")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment