A closure is actually a higher usage of a function type. Above, we just have two types of types; the closure is defined quite similarly to the function itself. This is because it takes the parameters and the area and the return type just as the type signage. This makes functions and closures siblings.
Optional可选类型
swift中的Optional底层就是一个带泛型的枚举类型。
其源码抽象出来如下:
1
2
3
4
5
6
7
public enum Optional<Wrapped> : ExpressibleByNilLiteral {
case none
case some(Wrapped)
public init(_ some: Wrapped)
public init(nilLiteral: ())
}
1
2
3
4
5
6
7
8
// 此时定义中的Wrapped就是String类型
var myName : String? = "Walker"
print(myName)
print(myName!)
if let name = myName {
print(name)
}
Maybe you could buy me a cup of coffee.
Scan this qrcode
Open alipay app scan this qrcode, buy me a coffee!
Scan this qrcode
Open wechat app scan this qrcode, buy me a coffee!