《Swift從入門到精通》(三):枚舉及內(nèi)存布局

  • 枚舉的基本用法

enum Direction {
    case north
    case south
    case east
    case west
}
enum Direction {
    case north, south, east, west
}
// 以上兩種寫法是等價
var dir = Direction.west
dir = Direction.east
dir = .north
print(dir) // north
switch dir {
case .north:
    print("north")
case .south:
    print("south")
case .east:
    print("east")
case .west:
    print("west")
}
  • 關(guān)聯(lián)值(Associated Values)

有時將枚舉的成員值跟其他類型的值關(guān)聯(lián)存儲在一起,會非常有用

enum Score {
    case points(Int)
    case grade(Character)
}
var score = Score.points(90)
score = .grade("A")
switch score {
case .points(let i):
    print("points", i)
case let .grade(i):
   print("grade", i)

}
enum Date {
    case digit(year: Int, mouth: Int, day: Int)
    case string(String)
}
var date = Date.digit(year: 2021, mouth: 9, day: 27)
//date = .string("2021-09-27")
switch date {
case let .digit(a, b, c):
    print(a, b, c)
case .string(let a):
    print(a)
}
  • 原始值(Raw Values)

枚舉成員可以使用相同類型的默認(rèn)值預(yù)先對應(yīng),這個默認(rèn)值叫做原始值, 原始值不占用枚舉變量的內(nèi)存

//原始值
enum PokerSuit : Character {
    case spade = "??"
    case heart = "??"
    case diamond = "??"
    case club = "??"
}
var suit = PokerSuit.spade
print(suit)
print(suit.rawValue)
print(PokerSuit.club.rawValue)

enum Grade : String {
    case perfect = "A"
    case great = "B"
    case good = "C"
    case bad = "D"
}
print(Grade.perfect.rawValue)
print(Grade.great.rawValue)
print(Grade.good.rawValue)
print(Grade.bad.rawValue)
  • 隱式原始值(Implicitly Assigned Raw Values)

如果枚舉的原始值類型是Int、 String, Swift會自動分配原始值

enum Direction : String {
    case north = "north"
    case south = "south"
    case east = "east"
    case west = "west"
}
// 上面的枚舉變量的原始值等價下面
enum Direction : String {
    case north, south, east, west
}
print(Direction.north) // north
print(Direction.north.rawValue) // north
enum Season : Int {
    case spring, summer, autumn = 4, winter
}
print(Season.spring.rawValue) // 0
print(Season.summer.rawValue) // 1
print(Season.autumn.rawValue) // 4
print(Season.winter.rawValue) // 5
  • 遞歸枚舉(Recursive Enumeration)

indirect enum ArithExpr {
    case number(Int)
    case sum(ArithExpr, ArithExpr)
    case difference(ArithExpr, ArithExpr)
}
let five = ArithExpr.number(5)
let four = ArithExpr.number(4)
let two = ArithExpr.number(2)
let sum = ArithExpr.sum(five, four)
let diff = ArithExpr.difference(sum, two)
func cal(_ expr: ArithExpr) -> Int {
    switch expr {
    case let .number(value):
        return value
    case let .sum(left, right):
        return cal(left) + cal(right)
    case let .difference(left, right)
        return cal(left) - cal(right)
    }
}
cal(difference) // 7
  • 枚舉的內(nèi)存布局

enum TestEnum {
case test0(Int, Int, Int)
case test1(Int, Int)
case test2(Int)
case test3(Bool)
case test4
}
print(MemoryLayout<TestEnum>.size)      // 25 實際用到的內(nèi)存
print(MemoryLayout<TestEnum>.stride)    // 32 申請分配的內(nèi)存
print(MemoryLayout<TestEnum>.alignment) // 8 內(nèi)存對其參數(shù)
// 一共申請32個字節(jié),實際用到25個字節(jié),實際要用到的內(nèi)存要按關(guān)聯(lián)值占用到的最大的內(nèi)存,在本例中是test0最大,關(guān)聯(lián)3個Int類型,在64位系統(tǒng)中每個Int占用8個字節(jié)
// 因此要用24個字節(jié),另外要一個字節(jié)即第25個字節(jié)用來標(biāo)識枚舉成員值,后面的7個字節(jié)用來內(nèi)存補(bǔ)齊,以滿足內(nèi)存對齊
// 小端模式
// 01 00 00 00 00 00 00 00
// 02 00 00 00 00 00 00 00
// 03 00 00 00 00 00 00 00
// 00 (這一個字節(jié)標(biāo)識枚舉的成員值,0代表test0)
// 00 00 00 00 00 00 00
var t = TestEnum.test0(1, 2, 3)
// 小端模式
// 04 00 00 00 00 00 00 00
// 04 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 01 (這一個字節(jié)標(biāo)識枚舉的成員值,1代表test1)
// 00 00 00 00 00 00 00
t = .test1(4, 5)
// 小端模式
// 06 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 02 (這一個字節(jié)標(biāo)識枚舉的成員值,2代表test2)
// 00 00 00 00 00 00 00
t = .test2(6)
// 小端模式
// 01 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 03 (這一個字節(jié)標(biāo)識枚舉的成員值,3代表test3)
// 00 00 00 00 00 00 00
t = .test3(true)
// 小端模式
// 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 00 00 00 00 00 00 00 00
// 04 (這一個字節(jié)標(biāo)識枚舉的成員值,4代表test4)
// 00 00 00 00 00 00 00
t = .test4
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 枚舉 枚舉的基本用法 關(guān)聯(lián)值(Associated Values) 原始值(Raw Values) 枚舉成員可以使...
    lieon閱讀 227評論 0 1
  • 枚舉的基本用法 enum Direction { case north case south case e...
    5e4c664cb3ba閱讀 259評論 0 0
  • 枚舉及可選型是 Swift 中兩個很重要的概念,前者與 Objective-C 中的概念大不相同,后者完全不存在,...
    CodingIran閱讀 1,453評論 0 1
  • 枚舉的基本你用法 關(guān)聯(lián)值(Associated Values) 關(guān)聯(lián)值是直接存在枚舉變量的內(nèi)存里面的,這點要牢記,...
    RUNNING_NIUER閱讀 868評論 0 1
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險厭惡者,不喜歡去冒險,但是人生放棄了冒險,也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,874評論 0 4

友情鏈接更多精彩內(nèi)容