//永遠不會被內聯 (即便開啓了編譯器優化)
@inline(never) fun test() {ide
}優化
//開啓編譯器優化後, 即便代碼很長, 也會被內聯 (遞歸, 動態派發除外)
@inline(_always) func test() {blog
}遞歸
enum Sorce {
case points(Int)
case grade(Character)
}編譯器
var score = Sorce.points(50)
score = .grade("A")it
enum Direction: String {
case north = @"north"
case south = @"south"
case east = @"east"
case west = @"west"
}
等價於
enum Direction: String {
case north, south, east, west
}io
enu m Direction: String {
case north = @"1"
case south = @"2"
case east = @"3"
case west = @"4"
}編譯
print(Direction.north) //north
print(Direction.north.rawValue) //1ast
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)
}test
var sum = ArithExpr(ArithExpr.number(5), ArithExpr,number(7)) //12
var difference = ArithExpr(ArithExpr.number(7), ArithExpr,number(5)) //2
MemoryLayout
MemoryLayout
MemoryLayout
MemoryLayout
var age: Int?
var num: Int = age!
let a: Int? =1
let b: Int? =2
let c = a??b
當guard語句的條件爲false時,會執行大括號裏面的代碼
當guard語句的條件爲true時,跳過guard語句
guard語句特別適合「提早退出」
var num: Int! = 10 var tmp = num