Swift枚舉的全用法

鑑於昨天開會部門會議討論的時候,發現有些朋友對枚舉的用法仍是存在一些疑問,因此就寫下這個文章,介紹下Swift下的枚舉的用法。javascript

基本的枚舉類型

來,二話不說,咱們先貼一個最基本的枚舉:php

enum Movement { case letf case right case top case bottom } 

這裏就定義了一個簡單的方向枚舉,有上下左右四個方面的case。那麼咱們能夠作些什麼操做呢?
一、你能夠遍歷他的枚舉:java

let aMovement = Movement.left switch aMovement { case .left: print("Left") default:() } 

二、你能夠對場景進行比較:bash

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

枚舉值

與OC不同,Swift的枚舉牛逼得多了,OC只能玩Int,他能玩:數據結構

  • 整型(Integer)
  • 浮點數(Float Point)
  • 字符串(String)
  • 布爾類型(Boolean)
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來構造一個枚舉對象,他是有可能不在任何一個場景的,由於,他的返回值是可選的。
ui

image.png

嵌套枚舉

enum Area { enum DongGuan { case NanCheng case DongCheng } enum GuangZhou { case TianHe case CheBei } } print(Area.DongGuan.DongCheng) 

上代碼,一目瞭然,怎麼調,直接看,哈哈~~~spa

關聯值

這個關聯值的說法就比較學術了,其實很簡單,咱們平時也常常用:code

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裏面,要寫這樣的代碼,麻煩了去了。orm

方法和屬性

先上代碼:對象

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等,而後根據不一樣的場景返回不一樣的值。

屬性

增長一個存儲屬性到枚舉中不被容許,但你依然可以建立計算屬性。固然,計算屬性的內容都是創建在枚舉值下或者枚舉關聯值獲得的。

enum Device { case iPad, iPhone var year: Int { switch self { case iPhone: return 2007 case iPad: return 2010 } } } 

靜態方法

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是一個以打印爲目的的自定義格式化輸出的類型。

protocol CustomStringConvertible {
  var description: String { get } } 

該協議只有一個要求,即一個只讀(getter)類型的字符串(String類型)。咱們能夠很容易爲enum實現這個協議。

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內容,緊接着轉移到方法定義:

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()) 

泛型

enum Rubbish<T> { case price(T) func getPrice() -> T { switch self { case .price(let value): return value } } } print(Rubbish<Int>.price(100).getPrice()) 
相關文章
相關標籤/搜索