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.
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.
This program specifies a Size
enum
, with the cases Small, Medium and Large. We use the "enum
" and case keywords to define an enum
.
enum
value that equals Size.Medium
. We then test this against the ".Medium" value.enum
name, and just test against ".Medium," after the type variable type is known.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.
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
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
.
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
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.
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!
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
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.
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.