【swift3.0】【枚举定义的不同方式】
生活随笔
收集整理的這篇文章主要介紹了
【swift3.0】【枚举定义的不同方式】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
貢獻作者 -【XJDomain】
博客XJ:? https://my.oschina.net/shengbingli/blog
GitHub直播地址:? https://github.com/lishengbing/XJDomainLive
XJ--01
> 定義字符串類型的枚舉
enum kBackgroundImageNameType : String {// 藍色<默認>case navBarBg_04BEC6 = "navBarBg_04BEC6"// 橘色case navBarBg_FFAB6D = "navBarBg_FFAB6D"// 灰色case navBarBg_E7E7E7 = "navBarBg_E7E7E7" }使用一:
kBackgroundImageNameType.navBarBg_04BEC6.rawValue?
?
XJ--02
> 定義沒有類型的枚舉:
enum MethodType {case getcase post }使用二:
let method = type == .get ? HTTPMethod.get : HTTPMethod.post?
XJ--03
> 枚舉的完美解析:
// 1:枚舉類型的定義 enum MethodType {case getcase postcase putcase delete }enum MethodType1 : String {case get = "get"case post = "post"case put = "put"case delete = "delete" }// 2:創建枚舉具體的值 let type1 : MethodType = .get let type2 = MethodType.post// 3:給枚舉類型進行賦值 // 如果枚舉沒有賦值的話是沒有值的,不像oc中默認依次是0.1.2.3.... enum Direction : Int {case east = 0case west = 1case north = 2case south = 3 }let d : Direction = .east // 只有賦值才會有這種創建方式:可選類型 let d1 = Direction(rawValue: 1)// 4:枚舉類型定義方式二 enum Type {case get, post, put, delete } // 只針對Int類型,會自動為0.1.2.3 enum Type1 : Int {case get = 0, post, put, delete }let type111 = Type1(rawValue: 1) print(type111)?
?
?
?
?
?
?
轉載于:https://my.oschina.net/shengbingli/blog/806307
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的【swift3.0】【枚举定义的不同方式】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: React从入门到精通系列之(12)深入
- 下一篇: Qt常见问题