Last active
January 26, 2023 14:46
-
-
Save clarkeben/866598d4aa41af8f174ce14b360cec46 to your computer and use it in GitHub Desktop.
Enumerations (enum) in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Associated Values | |
enum FavouriteAppleProducts { | |
case iphone(String) | |
case macbookPro(String) | |
case iPod(String) | |
} | |
let myFavouriteIpod = FavouriteAppleProducts.iphone("iPhone 14 pro") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - CaseIterable | |
enum Fruits: CaseIterable { | |
case orange, apple, pear, mango | |
} | |
for fruit in Fruits.allCases { | |
print("I like to eat \(fruit)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Example Enum | |
enum Compass { | |
case north | |
case east | |
case south | |
case west | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Colours: String { | |
case red = "red" | |
case blue = "blue" | |
case green = "green" | |
case orange = "orange" | |
case purple = "purple" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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