枚举

@inline

//永远不会被内联 (即使开启了编译器优化)
@inline(never) fun test() {

}

//开启编译器优化后, 即使代码很长, 也会被内联 (递归, 动态派发除外)
@inline(_always) func test() {

}

枚举

关联值

enum Sorce {
case points(Int)
case grade(Character)
}

var score = Sorce.points(50)
score = .grade("A")

原始值

enum Direction: String {
case north = @"north"
case south = @"south"
case east = @"east"
case west = @"west"
}
等价于
enum Direction: String {
case north, south, east, west
}

rawValue

enu m Direction: String {
case north = @"1"
case south = @"2"
case east = @"3"
case west = @"4"
}

print(Direction.north) //north
print(Direction.north.rawValue) //1

递归枚举

indirect enum ArithExpr {
case number(Int)
case sum(ArithExpr, ArithExpr)
case difference(ArithExpr, AirthExpr)
}
等价于
enum ArithExpr {
case number(Int)
indirect case sum(ArithExpr, ArithExpr)
indirect case difference(ArithExpr, AirthExpr)
}

var sum = ArithExpr(ArithExpr.number(5), ArithExpr,number(7)) //12
var difference = ArithExpr(ArithExpr.number(7), ArithExpr,number(5)) //2

MemoryLayout

MemoryLayout //范型
MemoryLayout .size
MemoryLayout .stride
MemoryLayout .alignment

可选项

var age: Int?

强制解包

var num: Int = age!

空合并运算符

let a: Int? =1
let b: Int? =2
let c = a??b

guard 语句

枚举_第1张图片
当guard语句的条件为false时,会执行大括号里面的代码
当guard语句的条件为true时,跳过guard语句
guard语句特别适合“提前退出”
枚举_第2张图片

隐式解包

var num: Int! = 10
var tmp = num

你可能感兴趣的:(枚举)