Enums
In Swift, an enumeration (enum) is a collection of set values that can be referenced throughout a program.
Syntax
enum MyEnum {
Enumeration cases go here
}
To access specific cases in MyEnum
, the dot operator .
is used, following the case name:
MyEnum.case
Creating an enum
and Examples
enum
cases can be defined on separate lines:
enum Level {case beginnercase intermediatecase advanced}
enum
cases can also be written in a single line for simplicity:
enum Level {case beginner, intermediate, advanced}
Raw Value
Raw values can be added for each case if a raw type is defined after the enum
name and trailing colon :
.
enum Bishop: String {case novice = "Beginner"case firstJob = "Magician"case secondJob = "Cleric"case thirdJob = "Priest"}
The example above displays an enum
, named Bishop
, with a raw type of String
and cases assigned to their respective raw values.
Associated Value
Associated values can be added to available cases.
enum MageAdvancementTree {case novicecase firstJob(String)case secondJob(wizard: String)case thirdJob(mage: String)case fourthJob(arch: String)}var bishop = MageAdvancementTree.thirdJob(mage: "Priest")print(bishop)// Output: thirdJob(mage: "Priest")
An enum
named MageAdvancementTree
is created and most of its cases have associated values. Then, a bishop
variable is declared as a MageAdvancementTree
and sets the mage
to a string, "Priest"
.
Accessing an enum
and Examples
An enumeration can be accessed with the dot .
syntax.
enum MageAdvancementTree {case novice, firstJob, secondJob, thirdJob, fourthJob}var bishop = MageAdvancementTree.thirdJobbishop = .fourthJobprint(bishop)// Output: fourthJob
When a variable is declared and set to an enumeration, it can be re-assigned to a different case value with the shortened dot syntax shown above.
Switch Case
A switch
statement is another way to access values within an enumeration. In the example below, the bishop
variable will be evaluated to match case .fourthJob
because it was previously set to the fourthJob
value. Access to the values in enum MageAdvancementTree
is possible because bishop
is already set to it.
switch bishop {case .novice:print("Beginner")case .firstJob:print("Magician")case .secondJob:print("Cleric")case .thirdJob:print("Priest")case .fourthJob:print("Bishop")}// Output: Bishop
Iterative
In the snippet below, MageAdvancementTree
adopts the CaseIterable
protocol which gives access to the allCases
property. Using a for...in
loop, each value within the enumeration can be accessed:
enum MageAdvancementTree: String, CaseIterable {case novice = "Beginner"case firstJob = "Magician"case secondJob = "Cleric"case thirdJob = "Priest"case fourthJob = "Bishop"}for job in MageAdvancementTree.allCases {print(job.rawValue)}
This would output the raw values in MageAdvancementTree
:
BeginnerMagicianClericPriestBishop
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours