基础语法-枚举

枚举的基本用法

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)

 

switch dir {

case .north:

    print("north")

case .south:

    print("south")

case .east:

    print("east")

case .west:

    print("west")

}

 

关联值

有时会将枚举的成员值跟其他类型的关联存储在一起,会非常有用

enum Score {

    case point(Int)

    case grade(Character)

}

var score = Score.point(96)

score = .grade("A")

switch score {

case let .point(i):

    print(i, "points")

case let .grade(i):

    print("grade", i)

}

 

enum Date {

    case digit(year: Int, month: Int, day: Int)

    case string(String)

}

var date = Date.digit(year: 2011, month: 9, day: 10)

date = .string("2011-09-10")

switch date {

case .digit(let year, let month, var day):

    day += 10

    print(year, month, day)

case let .string(value):

    print(value)

}

必要时let也可以改为var

 

关联值举例

手机密码

数字密码或者手势密码

enum Password {

    case number(Int, Int, Int, Int)

    case gesture(String)

}

 

var pwd = Password.number(3, 5, 7, 8)

pwd = .gesture("12369")

 

switch pwd {

case let .number(n1, n2, n3, n4):

    print("number is", n1, n2, n3, n4)

case let .gesture(str):

    print("gesture is", str)

}

 

原始值

枚举成员可以使用相同类型的默认值预先关联,这个默认值叫做:原始值

enum PokerSuit : Character {

    case spade = "♠"

    case heart = "♥"

    case diamond = "💎"

    case club = "♣"

}

var suit = PokerSuit.spade

print(suit)//spade

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)

 

隐式原始值

如果枚举的原始值是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, winter

}

print(Season.spring.rawValue) //0

print(Season.summer.rawValue) //1

print(Season.autumn.rawValue) //2

print(Season.winter.rawValue) //3

 

enum Season: Int {

    case spring=1, summer, autumn=4, winter

}

print(Season.spring.rawValue) //0

print(Season.summer.rawValue) //2

print(Season.autumn.rawValue) //4

print(Season.winter.rawValue) //5

 

递归枚举

indirect enum ArithExpr {

    case number(Int)

    case sum(ArithExpr, ArithExpr)

    case difference(ArithExpr, ArithExpr)

}

enum ArithExpr {

    case number(Int)

    indirect case sum(ArithExpr, ArithExpr)

    indirect 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 difference = ArithExpr.difference(sum, two)

 

func calculate(_ expr: ArithExpr) -> Int {

    switch expr {

    case let .number(value):

        return value

    case let .sum(left, right):

        return calculate(left) + calculate(right)

    case let .difference(left, right):

        return calculate(left) - calculate(right)

    }

}

calculate(difference)//7

 

MemoryLayout

可以使用MemoryLayout获取数据类型占用的内存大小

var age = 10

MemoryLayout<Int>.size //8

MemoryLayout<Int>.stride //8

MemoryLayout<Int>.alignment //8

MemoryLayout.size(ofValue: age)//8

MemoryLayout.stride(ofValue: age)//8

MemoryLayout.alignment(ofValue: age)//8

 

enum Password {

    case number(Int, Int, Int, Int) //32

    case other //1

}

var pwd = Password.number(5, 6, 7, 8)

MemoryLayout<Password>.size //33, 实际用到的空间大小

MemoryLayout<Password>.stride //40, 分配占用的空间大小

MemoryLayout<Password>.alignment //8,对齐参数

 

enum Season : String {

    case spring = "1", summer, autumn, winter

}

var s = Season.spring

MemoryLayout<Season>.size //1

MemoryLayout<Season>.stride //1

MemoryLayout<Season>.alignment //1

一个字节就够了(原因是关联值需要自己的内存来存储,原始值是固定死的,并不是存在变量里面)

posted @ 2019-12-08 19:05  do+better  阅读(385)  评论(0编辑  收藏  举报