主要内容
- 字面量
- 模式匹配
1. 字面量
1.1 字面量类型
Swift提供了很多的字面量,系统自带的很多类型都通过遵守字面量协议来实现字面量直接初始化
常见字面量的默认类型:
- public typealias IntegerLiteralType = Int
- public typealias FloatLiteralType = Double
- public typealias BooleanLiteralType = Bool
- public typealias StringLitteralType = String
修改字面量的默认类型:
// 可以通过typealias修改字面量的默认类型
typealias FloatLiteralType = Float
typealias IntegerLiteralType = UInt8
var age = 10 // UInt8
var height = 1.68 // Float
Swift自带的绝大部分类型,都支持直接通过字面量进行初始化,Bool、Int、Float、Double、String、Array、Dictionary、Set、 Optional等
1.2 字面量协议
常见字面量协议:
类型 | 协议 |
---|---|
Bool : | ExpressibleByBooleanLiteral |
Int : | ExpressibleByIntegerLiteral |
Float、Double : | ExpressibleByIntegerLiteral、ExpressibleByFloatLiteral |
Dictionary : | ExpressibleByDictionaryLiteral |
String : | ExpressibleByStringLiteral |
Array、Set : | ExpressibleByArrayLiteral |
Optional : | Expressibl |
应用:
extension Int : ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = value ? 1 : 0
}
}
var num: Int = true
print("WY:",num)//WY: 1
2. 模式匹配
模式是用于匹配的规则,比如switch的case、捕捉错误的catch、if\guard\while\for语句的条件等
模式种类:
- 通配符模式(Wildcard Pattern)
- 标识符模式(Identifier Pattern)
- 值绑定模式(Value-Binding Pattern)
- 元组模式(Tuple Pattern)
- 枚举Case模式(Enumeration Case Pattern)
- 可选模式(Optional Pattern)
- 类型转换模式(Type-Casting Pattern)
- 表达式模式(Expression Pattern)
通配符模式:
- _匹配任何值
- _?匹配非nil值
标识符模式:
- 给对应的变量、常量名赋值
值绑定模式:
//值绑定模式
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
// 原来的写法
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]")
//等价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)
}
}
- 这三者是等价的,if case语句等价于只有1个case的switch的语句
可选模式:
//可选模式
func test3() {
let age: Int? = 42
if case .some(let x) = age {
print(x)
}
if case let x? = age {
print(x)
}
}
类型转换模式:
//类型转换模式
func test4() {
let num: Any = 6
switch num {
case is Int:
// 编译器虽然可以判断Int型,依然认为num是Any类型
print("is Int", num)
//此时就会直接强转成Int型
//case let n as Int:
// print("as Int", n + 1)
default:
break
}
}
表达式模式:
//表达式模式
func test5() {
let point = (1, 2)
switch point {
case (0, 0):
print("(0, 0) is at the origin.")
//这里就是表达式模式
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
}
}