A ‘Swift’ Study of Enums

Navati
Feb 18, 2021

--

A quick review of enum concepts in Swift via code examples

Photo by Christiana Rivers on Unsplash
 case .begin

Enums are a type safe way to deal with related values

Syntax

Switch Statements

Raw Values

Raw Values: Implicitly Assigned Values

You need to specify the raw value type before accessing it

Invalid without specified raw type

Default Values for Int

Default Values for Int

Notice what happens when you assign a value to one case

Initialization from Raw Value

👀 Note: Raw Value initializer is a failable initializer !
Note the nil example!!

Associated Enums

Note how the associated value can be used or even omitted in different ways in a switch case:

Iterating

Implement ‘CaseIterable’ in the definition of the enum or even an extension of the enum

case .theEnd

--

--

Navati

iOS dev eager to learn + advocate for unrepresented folk in tech. Legacy Leadership Fellow @ WomenWhoCode Mobile + WTM Ambassador.