主要內(nèi)容
- 字面量
- 模式匹配
1. 字面量
1.1 字面量類型
Swift提供了很多的字面量球涛,系統(tǒng)自帶的很多類型都通過(guò)遵守字面量協(xié)議來(lái)實(shí)現(xiàn)字面量直接初始化
常見字面量的默認(rèn)類型:
- public typealias IntegerLiteralType = Int
- public typealias FloatLiteralType = Double
- public typealias BooleanLiteralType = Bool
- public typealias StringLitteralType = String
修改字面量的默認(rèn)類型:
// 可以通過(guò)typealias修改字面量的默認(rèn)類型
typealias FloatLiteralType = Float
typealias IntegerLiteralType = UInt8
var age = 10 // UInt8
var height = 1.68 // Float
Swift自帶的絕大部分類型,都支持直接通過(guò)字面量進(jìn)行初始化,Bool、Int、Float、Double、String哭懈、Array、Dictionary茎用、Set遣总、 Optional等
1.2 字面量協(xié)議
常見字面量協(xié)議:
類型 | 協(xié)議 |
---|---|
Bool : | ExpressibleByBooleanLiteral |
Int : | ExpressibleByIntegerLiteral |
Float、Double : | ExpressibleByIntegerLiteral轨功、ExpressibleByFloatLiteral |
Dictionary : | ExpressibleByDictionaryLiteral |
String : | ExpressibleByStringLiteral |
Array旭斥、Set : | ExpressibleByArrayLiteral |
Optional : | Expressibl |
應(yīng)用:
extension Int : ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = value ? 1 : 0
}
}
var num: Int = true
print("WY:",num)//WY: 1
2. 模式匹配
模式是用于匹配的規(guī)則,比如switch的case夯辖、捕捉錯(cuò)誤的catch琉预、if\guard\while\for語(yǔ)句的條件等
模式種類:
- 通配符模式(Wildcard Pattern)
- 標(biāo)識(shí)符模式(Identifier Pattern)
- 值綁定模式(Value-Binding Pattern)
- 元組模式(Tuple Pattern)
- 枚舉Case模式(Enumeration Case Pattern)
- 可選模式(Optional Pattern)
- 類型轉(zhuǎn)換模式(Type-Casting Pattern)
- 表達(dá)式模式(Expression Pattern)
通配符模式:
- _匹配任何值
- _?匹配非nil值
標(biāo)識(shí)符模式:
- 給對(duì)應(yīng)的變量、常量名賦值
值綁定模式:
//值綁定模式
let point = (3, 2)
switch point {
case let (x, y):
print("The point is at (\(x), \(y)).")
}
元組模式:
//元組模式
func test1() {
let name: String? = "jack"
let age = 18
let info: Any = [1, 2]
switch (name, age, info) {
case (_?, _ , _ as String):
print("case")
default:
print("default")
} // default
}
枚舉Case模式:
//枚舉Case模式
func test2() {
let age = 2
// 原來(lái)的寫法
if age >= 0 && age <= 9 {
print("[0, 9]")
}
// 枚舉Case模式
if case 0...9 = age {
print("[0, 9]")
}
guard case 0...9 = age else { return }
print("[0, 9]")
//等價(jià)switch case
switch age {
case 0...9: print("[0, 9]")
default: break
}
//for case
let ages: [Int?] = [2, 3, nil, 5]
for case nil in ages {
print("有nil值")
break
} // 有nil值
let points = [(1, 0), (2, 1), (3, 0)]
for (x, y) in points {
print(x, y)
}
for (x, _) in points {
print(x)
}
//帶0蒿褂,必須加case
for case let (x, 0) in points {
print(x)
}
}
- 這三者是等價(jià)的圆米,if case語(yǔ)句等價(jià)于只有1個(gè)case的switch的語(yǔ)句
可選模式:
//可選模式
func test3() {
let age: Int? = 42
if case .some(let x) = age {
print(x)
}
if case let x? = age {
print(x)
}
}
類型轉(zhuǎn)換模式:
//類型轉(zhuǎn)換模式
func test4() {
let num: Any = 6
switch num {
case is Int:
// 編譯器雖然可以判斷Int型,依然認(rèn)為num是Any類型
print("is Int", num)
//此時(shí)就會(huì)直接強(qiáng)轉(zhuǎn)成Int型
//case let n as Int:
// print("as Int", n + 1)
default:
break
}
}
表達(dá)式模式:
//表達(dá)式模式
func test5() {
let point = (1, 2)
switch point {
case (0, 0):
print("(0, 0) is at the origin.")
//這里就是表達(dá)式模式
case (-2...2, -2...2):
print("(\(point.0), \(point.1)) is near the origin.")
default:
print("The point is at (\(point.0), \(point.1)).")
} // (1, 2) is near the origin.
}
where:
//where模式
func test7() {
var data = (10, "Jack")
switch data {
case let (age, _) where age > 10:
print(data.1, "age>10")
case let (age, _) where age > 0:
print(data.1, "age>0")
default: break
}
}