枚举
作者:互联网
@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
MemoryLayout
MemoryLayout
可选项
var age: Int?
强制解包
var num: Int = age!
空合并运算符
let a: Int? =1
let b: Int? =2
let c = a??b
guard 语句
当guard语句的条件为false时,会执行大括号里面的代码
当guard语句的条件为true时,跳过guard语句
guard语句特别适合“提前退出”
隐式解包
var num: Int! = 10
var tmp = num
标签:case,north,Int,number,ArithExpr,枚举,var 来源: https://www.cnblogs.com/wwjwb/p/13467069.html