Enumerations are powerful language features that provide static typing for small set of frequently used values. They are used to make code more readable and less error-prone.
CaseIterable is a feature as when used on an enumeration, the compiler automatically generate a static collection of all the cases defined using the allCases property.
public protocol CaseIterable {
/// A type that can represent a collection of all values of this type.
associatedtype AllCases : Collection where Self == Self.AllCases.Element
/// A collection of all values of this type.
static var allCases: Self.AllCases { get }
}
enum CompassDirection: CaseIterable {
case north, south, east, west
// Code generated by the compiler
static var allCases: AllCases {
return [.north, .south, .east, .west]
}
//
}
Note that CaseIterable requirements are enumerations with no associated values or @available attributes on its cases.
Thanks 🙂