为什么枚举已经计算了属性,但没有存储在Swift中的属性? [英] Why do enums have computed properties but not stored properties in Swift?

查看:87
本文介绍了为什么枚举已经计算了属性,但没有存储在Swift中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


计算属性由类,结构和$提供, b $ b枚举。存储的属性仅由类和
结构提供。


为什么?枚举工作的相关值如存储的属性?看起来他们最初已经存储了属性 - 为什么没有存储类型的属性对于swift的课程?

解决方案

枚举已存储类型属性 - 即 static 属性。他们没有存储实例属性。我不知道为什么存储实例属性不可用于枚举的技术原因。如果您想要技术答案为为什么,您可能需要在开发者论坛上提出您的问题。



在您的问题中,您询问相关值是否与存储的属性一样工作。实际上,它们比 struct class 的存储属性更灵活(在某些方面) 。 enum 中的每个案例可以拥有自己的专用数据集。您可以使用一组适用于所有案例的存储属性,而不必为每个案例个性化存储的属性


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?

解决方案

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.

这篇关于为什么枚举已经计算了属性,但没有存储在Swift中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆