在 Swift 中,枚举(enum,enumeration 的缩写)是一种用户定义的,具有固定相关值的集合的数据类型。
我们使用 enum 关键字来创建枚举。例如:
enum Season {
case spring, summer, autumn, winter
}
这里,
Season- 枚举的名称spring/summer/autumn/winter- 枚举内部定义的值
注意:枚举的值也称为枚举成员(enum cases)。我们使用 case 关键字在枚举内部声明值。
创建枚举变量
由于枚举是一种数据类型,我们可以创建枚举类型的变量。例如:
var currentSeason: Season
在此,currentSeason 是一个 Season 类型的变量。它只能存储枚举内部存在的值(spring、summer、autumn、winter)。
给枚举变量赋值
我们使用枚举名称和 . 符号来给枚举变量赋值。例如:
// assign summer to currentSeason variable
var currentSeason = Season.summer
在这里,我们将成员值 summer 赋值给了枚举变量 currentSeason。
示例:Swift 枚举
// define enum
enum Season {
// define enum values
case spring, summer, autumn, winter
}
// create enum variable
var currentSeason: Season
// assign value to enum variable
currentSeason = Season.summer
print("Current Season:", currentSeason)
输出
Current Season: summer
在上面的例子中:
Season- 一个包含 4 个值的枚举:spring、summer、autumn、wintercurrentSeason- 枚举类型的变量currentSeason = Season.summer- 将枚举值赋给currentSeason
注意:与变量类似,我们也可以在一行中创建枚举变量并赋值。例如:
var lastSeason = Season.spring
Swift 枚举与 Switch 语句
我们也可以在 Swift 中将枚举与 switch 语句结合使用。例如:
enum PizzaSize {
case small, medium, large
}
var size = PizzaSize.medium
switch(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 内的语句。
遍历枚举成员
在 Swift 中,我们使用 CaseIterable 协议来遍历枚举。例如:
enum Season: CaseIterable {
...
}
现在我们可以使用 allCases 属性来循环遍历枚举的每个成员。
for currentSeason in Season.allCases {
...
}
示例:遍历枚举成员
// conform Languages to caseIterable
enum Season: CaseIterable {
case spring, summer, autumn, winter
}
// for loop to iterate over all cases
for currentSeason in Season.allCases {
print(currentSeason)
}
输出
spring summer autumn winter
在上面的示例中,我们将 CaseIterable 协议与 Season 枚举进行了关联。
然后,我们使用 allCases 属性配合 for 循环来遍历枚举的每个成员。
Swift 枚举的原始值(rawValue)
在 Swift 中,我们还可以为每个枚举成员分配值。例如:
enum Size : Int {
case small = 10
case medium = 12
...
}
在这里,我们分别为枚举成员 small 和 medium 分配了值 10 和 12。这些值被称为原始值(raw values)。
请注意,我们在枚举名称后使用了 Int 来定义枚举成员只能包含整数原始值。
访问枚举原始值
要访问枚举的原始值,我们使用 rawValue 属性。例如:
// access raw value
Size.small.rawValue
在这里,我们访问了 medium 成员的值。
示例:带原始值的枚举
enum Size : Int {
case small = 10
case medium = 12
case large = 14
}
// access raw value of python case
var result = Size.small.rawValue
print(result)
输出
10
在上面的示例中,我们创建了一个名为 Size 的枚举,其原始值的类型为 Int。
在这里,我们使用 rawValue 属性访问了 small 成员的值。
注意:原始值可以是字符串、字符、整数或浮点数类型。
Swift 枚举的关联值(Associated Values)
在 Swift 中,我们还可以为枚举成员附加额外的信息。例如:
enum Laptop {
// associate value
case name(String)
...
}
在这里,对于 name 成员,我们可以附加一个 String 类型的值。
现在,我们可以为该成员分配关联值。
Laptop.name("Razer")
在此,Razer 是 name 成员的关联值。
示例:枚举关联值
enum Laptop {
// associate string value
case name(String)
// associate integer value
case price (Int)
}
// pass string value to name
var brand = Laptop.name("Razer")
print(brand)
// pass integer value to price
var offer = Laptop.price(1599)
print(offer)
输出
name("Razer")
price(1599)
在上面的示例中,关联值
Razor被赋给了name成员。- 1599 被赋给了
price成员。
要了解更多关于关联值的信息,请访问Swift 枚举关联值。