字面量
- swift 自带的绝大部分类型,都支持直接通过字面量进行初始化
var age = 10
var isRed = false
var name = "Jack"
// 10 、false、Jack 就是字面量
public typealias IntegerLiteralType = Int
字面量协议
- Swift 自带类型之所以能够通过字面量初始化,是因为它们遵守了对应的协议 例如:
var age = 10 // ExpressibleByIntegerLiteral
- 字面量的应用
extension Int : ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = value ? 1 : 0
}
}
var num: Int = false
print(num) // 0
模式匹配
模式 (Pattern)
- 什么是模式
- 模式是用于匹配的规则,比如switch的case、捕捉错误的catch、if\guard\while\for语句的条件等
- Swift中的模式有很多,后面一个一个介绍
通配符模式(Wildcard Pattern)
enum Life {
case human(name: String, age: Int?)
case animal(name: String, age: Int?)
}
func check(_ life: Life) {
switch life {
case .human(let name, _):
print("human", name)
case .animal(let name, _?): // 如果去掉?就不会调用 default 后面的代码
print("animal",name)
default:
print("other")
}
}
check(.human(name: "Rose", age: 20)) // human Rose
check(.human(name: "Jack", age: nil)) // human Jack
check(.animal(name: "Dog", age: 5)) // animal Dog
check(.animal(name: "Cat", age: nil)) // other
标识符模式(Identifier Pattern)
值绑定模式 (Value-Binding Pattern)
let point = (3, 2)
switch point {
case let (x, y):
print(x,y)
}
元组模式 (Tuple Pattern)
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模式(Enumeration Case Pattern)
- if case 模式等价于只有一个 case 的 swiftch 语句
if age < 9 && age > 0 {
}
if case 0...9 = age {
}
switch age {
case 0...9:
print("0...9")
default: break
}
let ages: [Int?] = [2, 3, nil, 5]
for case nil in ages {
print("有 nil 值")
break
}
可选模式(Optional Pattern)
let age: Int? = 42
if case .some(let x) = age { print(x) }
if case let x? = age { print(x) }
类型转换模式(Type-Casting Pattern)
- 判断之后是不会进行类型强制转换的
- 在里面强制转换的话,用 case let num as Int 就好了
let num: Any = 6
switch num {
case is Int: // 编译器只会判断不会强转
print("is Int", num)
case let num as Int: // 编译器先判断能不能强转
default:
break
}
表达式模式(Expression Pattern)
- 用在 case 中
- 通过重载运算符
~=
,自定义表达式模式的匹配规则
struct Student {
var score = 0, name = ""
static func ~= (pattern: Int, value: Student) -> Bool {
value.score >= pattern
}
static func ~= (pattern: ClosedRange<Int>, value: Student) -> Bool {
pattern.contains(value.score)
}
static func ~= (pattern: Range<Int>, value: Student) -> Bool {
pattern.contains(value.score)
}
}
var stu = Student(score: 75, name: "Jack")
switch stu {
case 100: print("S")
case 90: print("A")
case 80..<90: print("B+")
case 60..<79: print("B")
case 0: print("D")
default: break
} // B
if case 60 = stu {
print(">= 60")
}// >= 60
var info = (Student(score: 70, name: "Jack"), "及格")
switch info {
case let (60, text):
print(text)
default:
break
}// 及格
func hasPrefix(_ prefix: String) -> ((String) -> Bool) {
// return {
// (str: String) -> Bool in
// str.hasPrefix(prefix)
// }
{ $0.hasPrefix(prefix) }
}
func hasSuffix(_ suffix: String) -> ((String) -> Bool) { { $0.hasSuffix(suffix) } }
var fn = hasPrefix("21")
print(fn("123231")) // false
print(hasSuffix("21")("22221")) // true
extension String {
static func ~= (pattern: (String) -> Bool, value: String) ->Bool {
pattern(value) // 调用发生在这
}
}
var str = "123456"
// 可以通过 switch 去判断
switch str {
case hasPrefix("12"), hasSuffix("56"): // 传了一个函数
print("以12开头或者以56结尾")
default:
break
} // 以12开头,以56结尾
where