枚舉

@inline

//永遠不會被內聯 (即便開啓了編譯器優化)
@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

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) //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 .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 語句

guard
當guard語句的條件爲false時,會執行大括號裏面的代碼
當guard語句的條件爲true時,跳過guard語句
guard語句特別適合「提早退出」
login

隱式解包

var num: Int! = 10 var tmp = num

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息