Adit Gupta Adit Gupta - 2 months ago 6
Swift Question

Why do enums have computed properties but not stored properties in Swift?

I am new to Swift and just came across this in the documentation:

Computed properties are provided by classes, structures, and
enumerations. Stored properties are provided only by classes and

Why is that? Do associated values for enum work like stored properties? It seems like they had stored properties initially - Why no stored type properties for classes in swift?


enums do have stored type properties - i.e., static properties. They don't have stored instance properties. I don't know if there is a technical reason why stored instance properties are not available for enums. You may have to ask your question on the dev forum if you want a technical answer for "why".

In your question you ask if associated values work like stored properties. In fact, they do, and are more flexible (in some ways) than stored properties for structs and classes. Each case in an enum can have its own specialized set of data that is associated with it. Rather than have one set of stored properties that apply to all cases, you get to individualize the stored properties for each case.