oc使用swift中的枚举
Enumeration is a data type that allows you to define a list of possible values. An enum allows you to create a data type with those set of values so that they can be recognised consistently throughout your app.
枚举是一种数据类型,它允许您定义可能值的列表。 枚举允许您使用这些值集创建数据类型,以便可以在整个应用程序中一致地识别它们。
In other word: It is a group of related values 💯
换句话说:它是相关值的组合
声明一个枚举 (Declaring An Enumeration)
To declare an enumeration, you list out all the possible member values as cases:
要声明一个枚举,请列出所有可能的成员值作为案例:
You can simplify the code a bit by collapsing the case clauses down to one line, with each value separated by a comma:
您可以通过将case子句折叠为一行,并用逗号分隔每个值来稍微简化代码:
This code creates a new enumeration called Month
with 12 possible member values. The commonly accepted best practice is to start each member value with a lower case first letter, just like a property.
Ť他的代码创建了一个名为新的枚举Month
12倍可能的成员的值。 普遍接受的最佳做法是像属性一样,以一个小写的首字母开头每个成员值。
了解枚举 (Understanding Enumeration)
The best way to use enum is through a switch
statement. Let’s make a function that demonstrates that:
使用枚举的最佳方法是通过switch
语句。 让我们做一个函数来说明这一点:
In the above code, in the functions parameter there is a
for
name just before the parameter is called. It is an internal name which we use inside a function and before internal name there is external name which we use out of function. Heremonth
is an internal name andfor
is an external name .在上面的代码中,在functions参数中,在调用参数之前有一个
for
名称。 它是我们在函数内部使用的内部名称,在内部名称之前是我们在函数外部使用的外部名称。 这里month
是一个内部名称,for
是一个外部名称。
Since Swift is strongly-typed and uses type inference, you can simplify semester(for:)
by removing the enumeration name in places where the compiler already knows the type. Keep the dot prefix, but lose the enumeration name, as shown below for the cases inside the switch statement:
由于Swift是强类型的并且使用类型推断,因此可以通过在编译器已经知道类型的地方删除枚举名称来简化semester(for:)
。 保留点前缀,但丢失枚举名称,如以下switch语句内的情况所示:
在Swift中迭代枚举案例 (Iterating Enum Cases in Swift)
In order to get all the cases available in a particular enum, we can use enum type CaseIterable
and can print using a for
loop. See the code for that below:
为了获得特定枚举中所有可用的案例 ,我们可以使用枚举类型CaseIterable
并可以使用for
循环进行打印。 请参见下面的代码:
Swift枚举中的原始值和关联值 (Raw Values and Associated Values in Swift Enum)
Raw Values : Attaching value with enum cases, and to access those we write dot raw value and can access those values which are with enum cases .
原始值 :将值附加在枚举案例中,要访问它们,我们可以编写点原始值,并且可以访问枚举案例中的那些值。
See the below example for raw values:
有关原始值,请参见以下示例:
In the above code, we create an enum named Fruits
and giving at rawtype String and giving raw values for every case. It’s not necessary to give rawValues
to every case. Then we made a function to get raw values.
在上面的代码中,我们创建了一个名为Fruits
的枚举 ,并为rawtype String给出了值,并为每种情况给出了原始值。 不必为每种情况都提供rawValues
。 然后,我们制作了一个函数来获取原始值。
If we make an enum as type CaseIterable then you can access all the cases using a for
loop and can count using allcases.count.
如果我们做一个枚举类型CaseIterable那么你就可以访问所有使用情况for
循环使用allcases.count可以指望。
Let’s see it by example:
让我们看一下它的例子:
In the above code, we add CaseIterable in its type then we can access the allCase and all case.count.
在上面的代码中,我们在其类型中添加CaseIterable ,然后我们可以访问allCase和所有case.count。
Associated Value : Associated values take Swift enumerations to the next level in expressive power. They let you associate a custom value (or values) with each enumeration case.
关联值 :关联值使Swift枚举的表现力更上一层楼。 它们使您可以将自定义值(或多个值)与每个枚举案例相关联。
In order to get a better understanding of associated values, we should go through their code example :
为了更好地理解关联值,我们应该看一下它们的代码示例:
It's not compulsory to associate values to every enum case in Swift. In the above code, we are making an enum called SocialMediaPaltaform
and checking for who is eligible for sponsorship by giving a criteria.
在Swift中,不必将值与每个枚举实例相关联。 在上面的代码中,我们将创建一个名为SocialMediaPaltaform
的枚举,并通过提供条件来检查谁有资格获得赞助。
An enumeration can have raw values or associated values, but not both.
枚举可以具有原始值或关联值,但不能同时具有两者。
关键点 (Key Points)
An enumeration is a list of mutually exclusive cases that define a common type.
枚举是定义共同类型的互斥情况的列表。
- Enumerations provide a type-safe alternative to old-fashioned integer values. 枚举为老式整数值提供了类型安全的替代方法。
- You can use enumerations to handle responses, store state, and encapsulate values. 您可以使用枚举来处理响应,存储状态和封装值。
- CaseIterable lets you loop through an enumeration with allCases. CaseIterable使您可以遍历allCases的枚举。
Author is available on LinkedIn: linkedin.com/in/my-pro-file
作者可以在LinkedIn上找到: linkedin.com/in/my-pro-file
翻译自: https://levelup.gitconnected.com/enumerations-in-swift-5dd14b4055a7
oc使用swift中的枚举