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

Adit Gupta picture Adit Gupta · Aug 28, 2015 · Viewed 25.5k times · Source

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 structures.

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?

Answer

Aaron Rasmussen picture Aaron Rasmussen · Aug 29, 2015

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.