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

后端 未结 4 1787
轻奢々
轻奢々 2020-12-08 18:58

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

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

4条回答
  •  北海茫月
    2020-12-08 19:53

    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.

提交回复
热议问题