When does Swift synthesize `CaseIterable` for an enum, and what do you do for an enum whose cases carry associated values?
answer
- opt in, never automatic
- no payloads, no availability attributes
- same file as the declaration
- declaration order, an array by default
- write allCases yourself otherwise
basics
~20 sSwift synthesizes allCases when an enum declares CaseIterable, has no associated values and no @available attributes on its cases, and the conformance is in the enum's own file. Otherwise you write static var allCases yourself.
solid answer
~40 s`CaseIterable` is opt-in: you write `enum Theme: CaseIterable` and the compiler generates `static var allCases`, an `[Theme]` in declaration order. Synthesis needs every case to be payload-free and none to carry an `@available` attribute; a payload case makes the compiler report that the type "does not conform to protocol 'CaseIterable'". The conformance can sit on the declaration or in an extension in the same file, but an extension in another file gets "extension outside of file declaring enum 'Theme' prevents automatic synthesis of 'allCases'". For a payload enum there is no single answer to "every value", so you either implement `allCases` by hand with the values you mean, or, more often, add a payload-free `Kind` enum that is `CaseIterable` and a computed `kind` property on the payload enum.
code
swift · 17 linesenum Theme: String, CaseIterable {
case light, dark, system
}
print(Theme.allCases.map(\.rawValue)) // ["light", "dark", "system"]
enum InboxFilter {
case all
case unread
case tagged(String)
}
// Not synthesized: tagged(String) carries a payload,
// so the conformance supplies allCases by hand.
extension InboxFilter: CaseIterable {
static let allCases: [InboxFilter] = [.all, .unread] + ["work", "home"].map(InboxFilter.tagged)
}
print(InboxFilter.allCases.count) // 4go deeper
Know that you opt in with : CaseIterable and then read Theme.allCases, an array of every case.
Explain the synthesis conditions: no payloads, no @available on cases, conformance in the enum's own file, and declaration order in the result.
Show how you handle a payload enum without a list that silently goes stale, typically a payload-free Kind enum plus a test over its cases.
Point out that allCases order becomes user-facing order, so reordering declarations is a product change that reviewers should see.
## What CaseIterable gives you `CaseIterable` is a standard-library protocol with one requirement: a static `allCases` property whose type is the associated type `AllCases`, a `Collection` of the enum's own values. The protocol gives `AllCases` a default of `[Self]`, so a synthesized conformance hands you an ordinary array. Typical uses: - populating a picker or segmented control with every option of `enum Theme { case light, dark, system }`; - counting the options with `Theme.allCases.count`; - writing a test that loops over every case, for example to check that each has a localized title or a unique raw value. Because the list is generated, adding a case to the enum adds it to `allCases` with no further edit. ## When the compiler writes allCases for you Synthesis is **opt-in**: no enum is `CaseIterable` until it declares the conformance. When it does, the compiler generates `allCases` only if all of these hold: 1. **No associated values.** A case such as `case loaded([String])` has unboundedly many values, so the compiler cannot list them, and the declaration fails with "type 'LoadState' does not conform to protocol 'CaseIterable'". 2. **No `@available` attributes on the cases.** The standard library's documentation for `CaseIterable` says synthesis applies to an enumeration without associated values or `@available` attributes on its cases. The Swift 6.4 compiler agrees: both `@available(*, unavailable)` and an OS-version attribute on one case make synthesis fail. 3. **The conformance is in the enum's own file.** Writing it on the declaration or in an extension in the same file works; an extension in another file of the same module fails with "extension outside of file declaring enum 'Theme' prevents automatic synthesis of 'allCases' for protocol 'CaseIterable'". A raw type is **not** required: `enum Beverage: CaseIterable { case coffee, tea, juice }` is fine. The synthesized array lists cases **in declaration order**, not sorted by name or raw value, so reordering the declaration reorders every picker built from it. ## Enums whose cases carry payloads For `enum InboxFilter { case all, unread, tagged(String) }` the question "what are all the values?" has no answer the compiler could know: every possible tag is a different value. You have two options: - **Write `allCases` yourself.** Declare the conformance and provide `static let allCases: [InboxFilter]` listing the values you actually mean, such as the built-in filters plus the user's saved tags. The protocol accepts any list, so the meaning is yours to document. - **Split out a payload-free kind.** Add `enum Kind: CaseIterable { case all, unread, tagged }` and a computed `var kind: Kind` on the payload enum. The kind drives the picker; the payload enum carries the data. This keeps the synthesized, always-complete list. The `kind` property is a `switch` over `self` that returns `.tagged` for every tag, so adding a payload case forces you to extend that `switch`, and the new kind appears in `Kind.allCases` automatically. A test that loops over `Kind.allCases` and checks each has a title then catches a missing localization the day the case is added. The second option is usually better, because a hand-written `allCases` does not grow when someone adds a case: the compiler cannot warn you that the list is now incomplete. ## Summary table | Enum | `allCases` | |---|---| | payload-free, declares `CaseIterable` | synthesized, declaration order | | payload-free, conformance in another file | compile error, write it yourself or move the extension | | a case with `@available` | compile error, write it yourself | | any case with associated values | compile error, write it yourself or add a `Kind` enum | | no `CaseIterable` declared | no `allCases` at all | ## What interviewers listen for A strong answer says the conformance is opt-in, names the payload rule, knows the order is declaration order, and has a plan for payload enums that does not silently go stale. A weak answer assumes every enum has `allCases`, or that it needs a raw type.
- Why can a hand-written `allCases` become a bug when the enum grows?The compiler only checks that `allCases` has the right type, not that it lists every case. Add `case archived` and the synthesized list picks it up automatically, while a hand-written array silently leaves it out. A payload-free `Kind` enum with synthesized `CaseIterable`, plus a test over it, keeps the list complete.
- Does `CaseIterable` require the enum to have a raw type?No. `CaseIterable` and `RawRepresentable` are independent: `enum Beverage: CaseIterable { case coffee, tea }` synthesizes `allCases` with no raw values at all. A raw type only adds `rawValue` and `init?(rawValue:)`, which is handy when the picker also needs a stored identifier.
- Every enum has `allCases` without declaring `CaseIterable`.
- `CaseIterable` is synthesized for enums with associated values too.
- `allCases` lists the cases sorted by name or raw value.
- An enum must have a raw type to conform to `CaseIterable`.
- Declaring `CaseIterable` in an extension in any file still synthesizes `allCases`.