Macros
The following macros are available globally.
-
This macro provides conformance of the
CaseIterableprotocol to enums, including those with associated values.In the example below, applying
@AutoCaseIterableto this enum provides automaticCaseIterableconformance, even though these cases have associated values.For associated values, default values are chosen in this order:
- A default value written on the case parameter (
case x(Foo = .bar)) is used verbatim. This is the recommended way to handle nested enums and Swift types which have no default init() available, such as URL. - Optional types use
nil. - Numeric types use
0. - String uses
"". - Bool uses
false. - Other types use
.init()(works for structs whose properties are all Optional or have default values).
Usage:
@AutoCaseIterable enum InterfaceType { case inputField(value: String) case modeButton(mode: Mode) case media(URL = URL(string: "https://apple.com")!) case divider }Declaration
Swift
@attached(extension, conformances: CaseIterable, names: named(allCases) ) public macro AutoCaseIterable() = #externalMacro(module: "AutomaticEnumCaseIterableMacro", type: "AutomaticEnumCaseIterableMacro") - A default value written on the case parameter (
View on GitHub
Macros Reference