Home
Map
enum ExamplesUse enums with cases and access rawValue. Test an enum in an if-statement and a switch.
Swift
This page was last reviewed on Dec 8, 2023.
Enum. Often values come in related groups—for example, a Size is small, medium or large. With a Swift enum, we create a safe enumeration of a group of values.
Advanced features. With enums, we can store unions of values. In a switch, we can use short syntax to match enums. Our enums can have Int, String values.
An example. This program specifies a Size enum, with the cases Small, Medium and Large. We use the "enum" and case keywords to define an enum.
Detail We create a constant enum value that equals Size.Medium. We then test this against the ".Medium" value.
var
Tip With enums, we can omit the enum name, and just test against ".Medium," after the type variable type is known.
Result The enum value equals ".Medium" so true is printed to the console with print().
enum Size { case Small case Medium case Large } // Create a value of Size enum type. let value = Size.Medium // Test the value. if value == .Medium { print(true) }
true
RawValue. We can specify a type for an enum—this is the raw value. Here we specify an enum that has Int values. We then access the rawValue from an enum value.
Tip The rawValue returns a value of the type specified in the enum. So here we get the Ints 0 and 1 from rawValue.
// Specify a raw value type of Int on the enum. enum Code: Int { case Ruby, Python } // Access a case from the enum. let c = Code.Ruby // ... Access the rawValue from the enum case. let raw = c.rawValue print(raw) // ... This case has a raw value 1 greater. let raw2 = Code.Python.rawValue print(raw2)
0 1
Init, rawValue. An enum variable can be created from a rawValue. Here we specify the rawValue of 4 (which matches Fish). This returns an Optional enum.
Optional
Here In the "if let" statement, we assign a variable to the optional's value. We then test the enum type.
enum Animal: Int { case Bird = 3 case Fish = 4 case Insect = 5 } // Create Animal enum from a rawValue Int of 4. if let temp = Animal(rawValue: 4) { if temp == Animal.Fish { // The rawValue is equal to Fish. print(true) print(temp.rawValue) } }
true 4
Switch. This program uses an enum in a switch. It specifies three cases in the enum Importance (on one line). The "Importance" part can be omitted in the switch cases.
switch
enum Importance { case Low, Medium, High } // Assign to an enum value. let value = Importance.High // Switch on the enum value. switch value { case .Low: print("Not important") case .Medium: print("Average") case .High: print("Do it now!") }
Do it now!
Default warning. In a switch, the Swift compiler will warn if a case is unreachable. It is safe to remove the "default" case if it will never be reached.
main.swift:16:1: Default will never be executed
Argument. An enum can be passed as an argument to a func. We use the enum type in the argument list. An enum in this respect is just like an Int, String or class type.
enum Importance { case Low, Medium, High } func process(i: Importance) { // Handle the enum argument in a func. if i == .Low || i == .Medium { print("Delay") } else { print("Immediate") } } // Call func with enum arguments. process(i: Importance.Low) process(i: Importance.Medium) process(i: Importance.High)
Delay Delay Immediate
ErrorType. In Swift, errors are specified as enums of type ErrorType. All exceptions thrown must be of this enum type. Enums have a core use in error handling in the language.
Error
A review. With enums, we encode "magic constants" or groups of possible values into a single unit. This can be passed to methods. Enums help enforce program correctness.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 8, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.