Adit Gupta Adit Gupta - 1 year ago 60
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download