Swift枚举笔记

来源:转载

//枚举示例(可以定义类型,例如:String)

enum NetworkConnection {

//枚举项可以定义参数 (也可以定义为Int或String类型)

case NotConnected

case WiFi(String,Int)

case Cellular(Int)

//定义函数执行

func description() ->String {

switch self {

case .NotConnected:

return "NotConnected"

case .WiFi(let wifiName, let pingTime):

return "WiFi [\(wifiName): \(pingTime)ms]"

case .Cellular(let pingTime):

return "Cellular \(pingTime)ms"

}

}

//预编译(mutating的意义是给自己赋值,description()这种函数是不能给自己赋值的)

mutating func lostConnection() {

self = .NotConnected

}

}

------------------------------------------------------------

print("\(currentConnection.description())")// 输出 WiFi [MyWiFi: 38ms]

currentConnection.lostConnection()

print("\(currentConnection.description())")// 输出 NotConnected

枚举也可以很屌,以上只是小case啦

分享给朋友:
您可能感兴趣的文章:
随机阅读: