枚举中使用枚举
In Swift, there are a lot of data structures, and they all have their own context when it’s needed to use exactly them. For instance, we use structures for describing an independent object, or closures which contains some functionality.There is one more interesting data structure in Swift language — enums. So let’s know what it exactly is and what functionality it gives us.
在Swift中,有很多数据结构,当需要精确使用它们时,它们都有各自的上下文。 例如,我们使用结构描述一个独立的对象,或者使用包含某些功能的闭包。在Swift语言中还有一个有趣的数据结构-枚举。 因此,让我们知道它到底是什么以及它为我们提供了什么功能。
什么是枚举 (What is enumeration)
Enum is a group of associated values of one type which are in a group by one logic sense. It may contain variants of network request’s result, types of users or even exist of value (optional, remember?). Likewise structs in Swift, enums are value-types which all benefits of this:
枚举是一种类型的一组关联值,按逻辑意义分组。 它可能包含网络请求结果的变体,用户类型或什至存在值(可选,还记得吗?)。 与Swift中的结构类似,枚举是值类型,它具有以下所有优点:
- They are stored in stack 它们存储在堆栈中
- They have no inheritance他们没有继承权
- They don’t require explicit initializer他们不需要显式的初始化程序
- They can conform to protocols它们可以符合协议
- It can have extensions它可以有扩展名
It even can have methods and computed properties(not stored!)
它甚至可以具有方法和计算属性(不存储!)
- And it can be used with generic types 它可以与泛型类型一起使用
Although we have said that it can not has stored properties, it can(and in most cases should) has rawValue: meaning of the case in current enum’s context.
尽管我们已经说过它不能具有存储的属性,但是它可以(并且在大多数情况下应该)具有rawValue:当前枚举上下文中大小写的含义。
Enums as data model
枚举作为数据模型
I’m going to demonstrate some cases of usage enums as data model by adding functionality gradually.Let’s imagine that we are need to describe types of waste. Also we want to know whether some type of waste are recyclable. For this, we can use conforming to protocol Recyclable:
我将通过逐步添加功能来演示一些使用枚举作为数据模型的案例,让我们想象一下我们需要描述浪费的类型。 我们也想知道某种废物是否可回收。 为此,我们可以使用符合协议Recyclable的协议:
Now we need to add one more detail: every waste type has its own `pollution value` — the indicator of how much devastated it is for our world. We can add it by declare raw value type:
现在,我们需要增加一个细节:每种废物类型都有自己的“污染值”,这是对我们的世界造成了多大破坏的指标。 我们可以通过声明原始值类型来添加它:
If enum’s raw value type is Int, Double, String or Character or any related to these types, it’s easy to define and use.
如果枚举的原始值类型是Int,Double,String或Character或与这些类型相关的任何值,则很容易定义和使用。
OK. But let’s say that the indicator is more complicated and declared by PollutionIndicatorModel. Is there any way to link enum’s case and the model? Sure!For this, we should conform our enum to RawRepresentable protocol and implement some fields:
好。 但是,可以说该指标更加复杂,并且由PollutionIndicatorModel声明。 有什么方法可以将枚举的情况与模型联系起来吗? 当然可以,我们应该使我们的枚举符合RawRepresentable协议并实现一些字段:
Now let’s add arguments to one of case, other, because it may have information about how the garbage is recycled. We have a bunch of ways, they all conform to RecyclingMechanism protocol, so we can use generic for define type of argument:
现在,让我们为一种情况(另一种情况)添加参数,因为它可能具有有关垃圾回收方式的信息。 我们有很多方法,它们都符合RecyclingMechanism协议,因此我们可以使用泛型来定义参数类型:
Of course, in the previous case we are able to use a protocol rather than generics. But with generic injection, we may define behavior for different types of recycling method.
当然,在前一种情况下,我们可以使用协议而不是泛型。 但是使用通用注入,我们可以为不同类型的回收方法定义行为。
Enum as a namespace
枚举作为名称空间
I also strong recommend you to use enums as a declaration for pretty separation of logic slices.
我也强烈建议您使用枚举作为逻辑片的漂亮分隔的声明。
Enums may have zero or more values
枚举可能具有零个或多个值
It means that we can create enum as a wrapper under another data structures:
这意味着我们可以在另一个数据结构下创建枚举作为包装器:
It’s a good way to structure your complicated data flow in your project’s module. Just one thing:
这是在项目模块中构造复杂数据流的好方法。 就一件事:
You can not declare protocols in enum
您不能在枚举中声明协议
Enums in recursion algorithms
递归算法中的枚举
I want to finish my research with interesting way to resolve algorithm’s tasks with recursion by merely defining it with the help of enums.What ? Recursion in value types? I know, it looks weird: to define value-type model’s size in stack swift compiler is needed to know its size. That’s the reason why we cannot use recursion in value types:
我想通过一种有趣的方式完成研究,通过仅借助枚举来定义递归来解决算法任务。 递归值类型? 我知道,这看起来很奇怪:在堆栈快速定义值类型模型的大小时需要知道其大小。 这就是为什么我们不能在值类型中使用递归的原因:
It’s possible, because indirect case is handled as a pointers to heap memory where its value is stored 🤯
这是有可能的,因为间接大小写被当作指向存储其值的堆内存的指针处理🤯
Conclusion
结论
Of course there are a lot another ways to use enums. For dive in deeper, I would recommend you a great book Advanced Swift where you can find much more about Swift language.
当然,还有很多使用枚举的方法。 为了更深入地学习,我会向您推荐一本很棒的书Advanced Swift ,您可以在其中找到有关Swift语言的更多信息。
Thank you for reading! Follow me in twitter and write your questions — I would be happy to answer 🤝
感谢您的阅读! 在Twitter上关注我并写下您的问题-我很乐意回答🤝
翻译自: https://medium.com/swlh/three-ways-to-use-enums-in-swift-21c51517162e
枚举中使用枚举