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?
enum
s 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 enum
s. 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 struct
s and class
es. 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 case
s, you get to individualize the stored properties for each case
.