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