枚举的特殊用法

枚举的特殊用法

源于项目中的应用太广了,但是对swift的很多简单特性不熟。之前那个动画的阶段使用到enum有遇到困难暂时绕开它了

当时的目标就是外部直接赋值操作

如下动画的enum示例:

为了省事,目前直接使用死的文案放在这里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
enum AnimationPeriod: UInt {
// 动画执行的五个阶段
case Start,First,Second,Third,End
func description() -> String {
switch self {
case .Start, .First: return "正在提取学校最新\n录取条件"
case .Second: return "正在与学校进行匹配"
case .Third, .End: return "正在根据匹配结果\n生成选校方案"
}
}
func duration() -> NSTimeInterval {
switch self {
case .Start: return 0.8
case .First: return 1
case .Second: return 2
case .Third: return 0.5
case .End: return 0.25
}
}
}
extension AnimationPeriod {
mutating func next() {
switch self {
case .Start: self = .First
case .First: self = .Second
case .Second: self = .Third
case .Third: self = .End
default: self = .End
}
}
}

下面我们看看一些枚举的使用

携带参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
enum FamilyType {
case Father(age: Int)
case Mother(age: Int)
case Sister(age: Int)
func gift() -> String {
switch(self) {
case .Sister(let age):
if age > 15 {
return "iphone"
} else {
return "toy"
}
case .Mother(let age) where age < 40:
return "cloth"
default:
return "book"
}
}
}
let someone = FamilyType.Sister(age: 11)
let somebody = FamilyType.Mother(age: 40)
// swift的枚举技巧
let someoneGift = someone.gift() // print toy
let somebodyGift = somebody.gift() // book

####那我们要取值的有

  • 方法一
1
2
3
4
5
6
7
switch someone {
case .Father(let age):
age
case .Sister(let age):
age
default:()
}
  • 方法二
1
2
3
if case .Sister(let age) = someone {
age
}

枚举的嵌套

1
2
3
4
5
6
7
8
9
10
11
enum Colleague {
enum Weight: Int {
case Light
case Mid
case Heavy
}
case iOS(weight: Weight)
case Android(weight: Weight)
case HTML(weight: Weight)
}
let woodenHelmet = Colleague.iOS(weight: .Mid)

枚举的成员变量

之前就一直想要使用枚举做到外部动态对枚举赋值储存操作,
那成员变量用法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 根据自身赋值
enum Device {
case iPad, iPhone
var year: Int {
switch self {
case iPhone: return 2007
case iPad: return 2010
}
}
}
// set get 方法对于枚举的成员变量是无效的,允许get调用但set不可执行
enum Book {
case Story, News
var year: Int {
set{
year = newValue
}
get{
return self.year
}
}
}
let myIpad = Device.iPad
myIpad.year
var storyBook = Book.Story
//storyBook.year = 2010 // set 方法此处报错
//storyBook.year // get 无效

初始化,感觉然并软

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
enum AppleDevice {
case iMac(price:Int)
case iPod(price:Int)
case iPhone(price:Int)
init (costMoney: Int) {
if costMoney > 10000 {
self = .iMac(price: costMoney)
} else if costMoney > 2500 {
self = .iPhone(price: costMoney)
} else {
self = .iPod(price: costMoney)
}
}
}
let myDevice = AppleDevice(costMoney: 6000)

元组的使用

多个参数的介入时,可以使用元组,此处常规使用报错暂时无法解决,
使用func函数赋值倒是没有出错。why??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
enum HumanHabit {
case Reading
case PlayGame
case Traveling
}
typealias HumanInfo = (age: Int, name: String, habit: HumanHabit)
func selectRAM(humanInfo: HumanInfo) -> HumanInfo {return (age: 32, name: humanInfo.name, habit: humanInfo.habit)}
func selectCPU(humanInfo: HumanInfo) -> HumanInfo {return (age: humanInfo.age, name: "3.2GHZ", habit: humanInfo.habit)}
func selectGPU(humanInfo: HumanInfo) -> HumanInfo {return (age: humanInfo.age, name: "3.2GHZ", habit: .Reading)}
enum Desktop {
case Cube(HumanInfo)
case Tower(HumanInfo)
case Rack(HumanInfo)
}
let aTower = Desktop.Tower(selectGPU(selectCPU(selectRAM((0, "", .Traveling) as HumanInfo))))