Swift 枚举
在本教程中,我们将通过实例了解 Swift 枚举(enumeration)。
在 Swift 中,枚举(enum 的缩写)是用户定义的数据类型,具有一组固定的相关值。
我们使用 enum 关键字创建一个枚举。例如,
enum Season {case spring, summer, autumn, winter}
这里,
- Season - 枚举的名称
- spring/summer/autumn/winter - 枚举内定义的值
case 关键字在枚举中声明值。创建枚举变量
由于枚举是一种数据类型,我们可以创建枚举类型的变量。例如,
var currentSeason: Season
这里,currentSeason 是一个 Season 类型变量。它只能存储枚举中的值(spring, summer, autumn, winter)。
为枚举变量赋值
我们使用枚举名和 . 符号将值赋给枚举变量。例如,
// assign summer to currentSeason variablevar currentSeason = Season.summer
这里,我们将成员值 summer 赋值给枚举变量 currentSeason。
实例:枚举
// define enumenum Season {// define enum valuescase spring, summer, autumn, winter}// create enum variablevar currentSeason: Season// assign value to enum variablecurrentSeason = Season.summerprint("Current Season:", currentSeason)
结果如下:
Current Season: summer
在上面的例子中,
Season - 包含 4 个值的枚举:spring, summer, autumn, winter
currentSeason - 枚举类型变量
currentSeason = Season.summer - 将枚举值分配给 currentSeason
var lastSeason = Season.spring
Swift 中带有 Switch 语句的枚举
我们还可以在 Swift 中使用带有 switch 语句 的枚举。例如,
enum PizzaSize {case small, medium, large}var size = PizzaSize.mediumswitch(size) {case .small:print("I ordered a small size pizza.")case .medium:print("I ordered a medium size pizza.")case .large:print("I ordered a large size pizza.");}
结果为:
I ordered a medium size pizza.
在上面的实例中,我们创建了一个名为 PizzaSize 的枚举,其值为:small、medium、large。请注意这一句,
var size = PizzaSize.medium
这里,我们将值 medium 赋值给枚举变量 size。
我们在 switch 语句中使用了枚举变量 size。并将该值与每个 case 语句的值进行比较。
由于该值与 case.medium 匹配,因此执行 case 中的语句。
由于该值与 case.medium 匹配,因此执行 case 中的语句。
遍历枚举事例
在 Swift 中,我们使用 CaseInterable 协议循环遍历枚举。例如,
enum Season: caseIterable {...}
现在,我们可以使用 allCases 属性循环遍历枚举的每个 case。
for currentSeason in Season.allCases {...}
实例:遍历枚举事例
// conform Languages to caseIterableenum Season: CaseIterable {case spring, summer, autumn, winter}// for loop to iterate over all casesfor currentSeason in Season.allCases {print(currentSeason)}
结果如下:
springsummerautumnwinter
在上面的例子中,我们将 CaseTerable 协议与枚举 Season 相一致。
然后,我们在 for 循环中使用 allCases 属性来迭代枚举的每个事例。
具有原始值(rawValue)的 Swift 枚举
在 Swift 中,我们还可以为每个枚举事例赋值。例如,
enum Size : Int {case small = 10case medium = 12...}
在这里,我们分别为枚举 small 和 medium 赋值 29 和 31。这些值称为原始值。
请注意,我们使 Int 和枚举名称来定义枚举事例只能包含整数原始值。
访问枚举原始值
要访问枚举的原始值,我们使用 rawValue 属性。例如,
// access raw valueSize.small.rawValue
实例:具有原始值的 Swift 枚举
enum Size : Int {case small = 10case medium = 12case large = 14}// access raw value of python casevar result = Size.small.rawValueprint(result)
结果如下:
10
在上面的实例中,我们创建了名为 Size 的枚举,该枚举具有 Int 类型的原始值。
这里,我们使用 rawValue 属性访问了 small 的值。
Swift 枚举关联值
在 Swift 中,我们还可以将附加信息附加到枚举 case。例如,
enum Laptop {// associate valuecase name(String)...}
在这里,对于 case name,我们可以附加一个 String 类型的值。
现在,我们可以为 case 赋值相关的值。
Laptop.name("Razer")
这里,Razer 是 name case 的关联值。
实例:枚举关联值
enum Laptop {// associate string valuecase name(String)// associate integer valuecase price (Int)}// pass string value to namevar brand = Laptop.name("Razer")print(brand)// pass integer value to pricevar offer = Laptop.price(1599)print(offer)
结果为:
name("Razer")price(1599)
在上面的实例中,关联的值
Razer 被赋值给
name case。1599 被赋值给
price case。
要了解有关关联值的更多信息,请访问 Swift 枚举关联值。