今天無心發現一個東西, 可是在文檔上看了不少遍都沒找到, 可是親測是可行的, 那究竟是什麼呢?swift
之前咱們定義枚舉 會這樣: spa
enum Hello { case Item( String, Int) case Healthy( Float, Float) }
文檔上也是這麼寫的,可是在開發中,例如: orm
enum FeastTransform { //(let cityID: Int?, let catoryID: Int?, let typeID: Int? , let sort: String?, let number: String?, let time: String, let cdbID: Int, let placeTypeID: Int, let banner: Int, let nextCursor: String, count:Int) case ThemesOfPublicT(Int, Int, Int, String, String, String, Int, Int, Int, String, Int) case ThemesOfPublic(String, Int?, ID , Count, List?) case Theme(ID) case ThemesOfMaster(ID, ID) case ThemesOfParticipator(ID, ID) case ThemesOfMyself(String?, ID, Count) case ThemesOfMasterBookable(ID) case ThemesUpdate(ID, String, String, String, String, String, DictArray, DictArray) }
爲了可讀性,咱們頂多作到就是用blog
typealias ID = String typealias Name = String typealias URL = String typealias Count = Int typealias Price = String typealias KeyWorkds = String typealias List = Array<String>
可是上面的可讀性仍是 so ugly!ip
可是今天無心發現這麼個東西,😄。真的不要謝我,真心是無心的。原來枚舉能夠這麼玩: ci
enum Hello { case Item(name: String, age: Int) case Healthy(height: Float, weight: Float) }
給他的參數命名, 玩過Haskell的夥伴應該說句 nice!這個真心nice,可讀性立馬 提高到一個水平, 都不用這個 `typealias KeyWorkds = String`。開發
extension Hello { func para() -> Dictionary<String, Any> { switch self { case .Item(let name , let age): return ["name": name, "age": age] case .Healthy(let height, let weight): return ["height": height, "weight":weight] } } } let a = Hello.Healthy(height: 12, weight: 13) a.para()
有興趣的夥伴們也能夠試試哇.文檔
使用Where語句: it
extension Media { var publishedAfter1930: Bool { switch self { case let .Book(_, _, year) where year > 1930: return true case let .Movie(_, _, year) where year > 1930: return true case .WebSite: return true // same as "case .WebSite(_)" but we ignore the associated tuple value default: return false } } }