protocol CaseIterable
Associated Types | |
---|---|
Import | import Swift |
Static Variables
static var allCases: Self.AllCases Required
A collection of all values of this type.
Declaration
static var allCases: Self.AllCases { get }
A type that provides a collection of all of its values.
Types that conform to the
CaseIterable
protocol are typically enumerations without associated values. When using aCaseIterable
type, you can access a collection of all of the type's cases by using the type'sallCases
property.For example, the
CompassDirection
enumeration declared in this example conforms toCaseIterable
. You access the number of cases and the cases themselves throughCompassDirection.allCases
.Conforming to the CaseIterable Protocol
The compiler can automatically provide an implementation of the
CaseIterable
requirements for any enumeration without associated values or@available
attributes on its cases. The synthesizedallCases
collection provides the cases in order of their declaration.You can take advantage of this compiler support when defining your own custom enumeration by declaring conformance to
CaseIterable
in the enumeration's original declaration. TheCompassDirection
example above demonstrates this automatic implementation.