Enumerations
Enumertaion Syntax ;
Matching Enumerations Values with a Switch Statement;
Iterating over Enumeration Cases;
Associated Values;
Raw Values;
Recursive Enumerations;
1.Enumeration Syntax
一般形式为 :
enum SomeEnumeration { //enumeration definition }
1.1
enum SomeEnum {
case one
case two
case three
}
// 多个case可以写在一行
enum SomeEnumInline {
case one , two , three ,
}
```swift
获得 enumeration中的值的常变量形式如下
> var se = SomeEnum.one | 只有一个enumeration时可简写为 se = .one
* * *
2.Matching Enumeration Values with a Switch Statement
```swift
var PrintNum = SomeEnum.two
switch PrintNum {
case .one :
print("one")
case .two :
print("two")
case .three :
print("three")
}
用switch匹配枚举的值时必须把每个case写出,如果不写出则报错,或者用 default 结尾
PrintNum = .one
switch PrintNum {
case .one :
print("one")
default :
print("remains")
}
3.Iterating over Enumeration Cases
two Keys : CaseIterable allCases
enum fourItem : CaseIterable {
case one , two , three
}
for i in fourItem.allcases {
print(i)
}
4.Associated values
case中的相关值可不同类型,并且在switch匹配值时可以被提出为常量或者变量
enum People {
case person ( Int , Int ,Int)
case sex (String)
}
let apple = People.person(116, 180, 6)
let appleSex = People.sex("male")
'associated values can be different'
case differ ( Int , Bool , String)
一个case中的类型都相同时,可把类型声明写在case后
switch apple {
case let People.person (value1 , value2 ,value3):
print("\(value1) , \(value2) , \(value3)")
case People.sex (let string1 , let value):
print("\(string1) , \(value)")
}
5.Raw values
· 重点, raw values类型的 枚举 ,里面case 的值类型必须一致
· 分为显式初始化值和隐式初始化值,显示如下
enum SomeCharacter : Character {
case a = "a"
case b = "b"
case c = "c"
}
implicit assigned raw values
常规的 let x = enumeration.case_1只能获得这个case
要获取case的raw value必须使用关键字 rawValue
case_1.rawValue
enum enumLikeCtype: Int {
case a = 1 , b ,c ,d ,e
}
let x = enumLikeCtype.b.rawValue
// x = 2
Initializing from a Raw Value , " enumeration(rawValue : Int) "
这里这种初始化值,括号中是可选类型,超出索引或不存在在序列中返回 nil
let CtypeRawPossibleValue = enumLikeCtype(rawValue : 3)
// c
if enumLikeCtype(rawValue:6) == nil {
return enumLikeCtype(rawValue:6)
}
"报错 :return invalid outside of a func , 超出索引则无值 "
- Recursive Enumerations
递归枚举,用关键字 indirect
indirect 可单独写在每个case前面 ,每个都带有raw values的case所在的enumeration可以把关键字indirect写在enum关键字的前面
enum arithmeticExpre {
case num (Int)
indirect case add ( arithmeticExpre , arithmeticExpre)
indirect case multiple ( arithmeticExpre , arithmeticExpre)
}
optional form : indirect enum arithmeticExpre { }
let five = arithmeticExpre.num(5)
let four = arithmeticExpre.num(4)
let addtionNum = arithmeticExpre.add(five, four)
let multipleNum = arithmeticExpre.multiple(addtionNum, four)
func EnumCalcu(_ expression : arithmeticExpre) -> Int {
switch expression {
case let .num(value):
return value
case let .add(left , right) :
return EnumCalcu(left) + EnumCalcu(right)
case let .multiple(left , right) :
return EnumCalcu(left) * EnumCalcu(right)
}
}
print(EnumCalcu(multipleNum))
enumeration case的相关值可以是 enumeration类型,递归函数EnumCalcu通过不断的讲enumeration式代入,直至得到switch 中.num的整型返回值,最后计算出相乘的结果。 enumeration 中存储的是计算式的表达式和操作,最后在函数中迭代至获得数值时再回退计算操作得到的值。