logo头像
Snippet 博客主题

Swift枚举的全用法

基本的枚举类型

一个最基本的枚举:

1
2
3
4
5
6
enum Movement {
case letf
case right
case top
case bottom
}

1、你可以遍历他的枚举:

1
2
3
4
5
6
7
8
let aMovement = Movement.left

switch aMovement {
case .left:
print("Left")
default:()

}

2、你可以对场景进行比较:

1
2
3
if aMovement == .left {
print("Left")
}

枚举值

与OC不一样,Swift的枚举牛逼得多了,OC只能玩Int,他能玩:

整型(Integer)
浮点数(Float Point)
字符串(String)
布尔类型(Boolean)

1
2
3
4
5
6
7
8
9
10
11
12
enum Movement:Int {
case left = 0
case right = 1
case top = 2
case bottom = 3
}

enum Area: String {
case DG = "dongguan"
case GZ = "guangzhou"
case SZ = "shenzhen"
}

不过,你要是想玩个自定义的类关联类型,那还是不行的,不过,已经够用了。如果你想要或者枚举的case对应的值,只需要print(Area.DG.rawValue)这样调用就可以了,直接调用对应的rawValue。不过有一点要注意哈,你如果用rawValue来构造一个枚举对象,他是有可能不在任何一个场景的,因为,他的返回值是可选的。

嵌套枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
enum Area {
enum DongGuan {
case NanCheng
case DongCheng
}

enum GuangZhou {
case TianHe
case CheBei
}
}

print(Area.DongGuan.DongCheng)

关联值

这个关联值的说法就比较学术了,其实很简单,我们平时也经常用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum Trade {
case Buy(stock:String,amount:Int)
case Sell(stock:String,amount:Int)
}

let trade = Trade.Buy(stock: "003100", amount: 100)

switch trade {
case .Buy(let stock,let amount):

print("stock:\(stock),amount:\(amount)")

case .Sell(let stock,let amount):
print("stock:\(stock),amount:\(amount)")
default:
()
}

你看,其实就是枚举的case可以传值,不要小看这功能,放在OC里面,要写这样的代码,麻烦了去了。

方法和属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum Device {
case iPad, iPhone, AppleTV, AppleWatch
func introduced() -> String {

switch self {
case .iPad: return "iPad"
case .iPhone: return "iPhone"
case .AppleWatch: return "AppleWatch"
case .AppleTV: return "AppleTV"
}
}
}

print(Device.iPhone.introduced())

很清晰,我们定义了一个设备枚举,有iPad, iPhone, AppleTV, AppleWatch,还有一个介绍的方法。这里的introduced方法,你可以认为枚举是一个类,introduced是一个成员方法,Device.iPhone就是一个Device的实例,case们是他的属性,好了,有了这个对像,Device.iPhone可以认为,Device里面有一个匿名属性,现在设置这个属性为iPhone。好了,introduced里面的switch self,其实就是遍历这个匿名属性的所有场景,如iPad,iPhone等,然后根据不同的场景返回不同的值。

属性

增加一个存储属性到枚举中不被允许,但你依然能够创建计算属性。当然,计算属性的内容都是建立在枚举值下或者枚举关联值得到的。

1
2
3
4
5
6
7
8
9
enum Device {
case iPad, iPhone
var year: Int {
switch self {
case iPhone: return 2007
case iPad: return 2010
}
}
}

静态方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum Device {
case iPad, iPhone, AppleTV, AppleWatch
func introduced() -> String {

switch self {
case .iPad: return "iPad"
case .iPhone: return "iPhone"
case .AppleWatch: return "AppleWatch"
case .AppleTV: return "AppleTV"
}
}

static func fromSlang(term: String) -> Device? {

if term == "iWatch" {

return .AppleWatch
}
return nil
}
}

print(Device.fromSlang(term: "iWatch"))

static func fromSlang(term: String) -> Device?就是一个静态方法。

协议

Swift也允许你在枚举中使用协议(Protocols)和协议扩展(Protocol Extension)。
Swift协议定义一个接口或类型以供其他数据结构来遵循。enum当然也不例外。我们先从Swift标准库中的一个例子开始.
CustomStringConvertible是一个以打印为目的的自定义格式化输出的类型。

1
2
3
protocol CustomStringConvertible {
var description: String { get }
}

该协议只有一个要求,即一个只读(getter)类型的字符串(String类型)。我们可以很容易为enum实现这个协议。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum Trade :CustomStringConvertible{
case Buy(stock:String,amount:Int)
case Sell(stock:String,amount:Int)

var description: String {

switch self {
case .Buy(_, _):
return "Buy"

case .Sell(_, _):
return "Sell"
}
}
}

print(Trade.Buy(stock: "003100", amount: 100).description)

扩展

枚举也可以进行扩展。最明显的用例就是将枚举的case和method分离,这样阅读你的代码能够简单快速地消化掉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
enum Device {
case iPad, iPhone, AppleTV, AppleWatch

}
extension Device: CustomStringConvertible{

func introduced() -> String {

switch self {
case .iPad: return "iPad"
case .iPhone: return "iPhone"
case .AppleWatch: return "AppleWatch"
case .AppleTV: return "AppleTV"
}
}

var description: String {

switch self {
case .iPad: return "iPad"
case .iPhone: return "iPhone"
case .AppleWatch: return "AppleWatch"
case .AppleTV: return "AppleTV"
}
}
}

print(Device.AppleTV.description)

print(Device.iPhone.introduced())

泛型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum Rubbish<T> {

case price(T)

func getPrice() -> T {

switch self {
case .price(let value):
return value
}

}
}

print(Rubbish<Int>.price(100).getPrice())
微信打赏

赞赏是不耍流氓的鼓励