typealias
用来给类型起别名
分别给 Int8
Int16
Int64
设置别名
typealias Byte = Int8
typealias Short = Int16
typealias Long = Int64
//调用
let value1: Byte = 8 //Byte 等价于 Int8 类型
let value2: Short = 16 //Byte 等价于 Int16 类型
let value3: Long = 64 //Byte 等价于 Int64 类型
给元组
起个别名 (year: Int, month: Int64, day: Int)
赋值给Date
typealias Date = (year: Int, month: Int64, day: Int)
func test(_ date: Date) {
print(date.0)
print(date.year)
}
test((2020, 4, 13))
给函数类型
起别名
typealias IntFn = (Int, Int) -> Int
func difference(v1: Int, v2: Int) -> Int {
v1 - v2
}
let fn: IntFn = difference
fn(20, 10) // 10
func setFn(_ fn: IntFn) { }
setFn(difference)
func getFn() -> IntFn {
difference
}
按照Swift
标准库定义,Void
就是空元组()
![](https://img.haomeiwen.com/i439166/2ea71d36ac276635.png)
网友评论