print(sortedCompany)
sortedCompany = []
+
+// 尾随闭包
+// 尾随闭包是一个书写在`函数括号之后`的闭包表达式,函数支持将其作为最后一个参数调用
+// 在使用尾随闭包时,不用写出它的参数标签
+
+printLine("Trailing Closures")
+sortedCompany = company.sorted() { $0 > $1}
+print(sortedCompany)
+sortedCompany = []
+
+// 如果闭包表达式是函数或方法的唯一参数,在使用尾随闭包时,可以把`()`省掉
+sortedCompany = company.sorted { $0 > $1 }
+print(sortedCompany)
+sortedCompany = []
+
+
/*
There’s actually an even shorter way to write the closure expression above. Swift’s String type defines its string-specific implementation of the greater-than operator (>) as a function that has two parameters of type String, and returns a value of type Bool. This exactly matches the function type needed by the sort(_:) method. Therefore, you can simply pass in the greater-than operator, and Swift will infer that you want to use its string-specific implementation:
*/
}
// 闭包是引用类型
-let closureFuncA = makeIncrementer(1)
+let closureFuncA = makeIncrementer(2)
print("ClosureFuncA:", closureFuncA())
print("ClosureFuncA:", closureFuncA())
let closureFuncB = closureFuncA
// 逃逸&非逃逸闭包
+// 当一个闭包作为参数传到一个函数中,但是这个闭包在函数返回之后才被执行,则称该闭包从函数中逃逸
+// 可以在定义参数时,在参数名前标注@escaping来指明这个闭包是允许逃逸出这个函数的
+
printLine("Noescaping & Escaping Closesure")
func noescapingClosure(_ closure: () -> Void) {
closure()
}
var closureHandler: Array<() -> Void> = []
-func escapingClosure(_ closure: @escaping () -> Void) { // 此时参数前加@noescape会报错
+func escapingClosure(_ closure: @escaping () -> Void) { // 此时参数前加@noescape会报错
closureHandler.append(closure)
}
var x = 10
func doSomethingAboutEscape() {
noescapingClosure() { x = 200 } // 将参数标记为@noescape能在闭包中隐式地引用self
- escapingClosure() { self.x = 100 }
+ escapingClosure() { self.x = 100 } // 将一个闭包参数标记为@escaping意味着必须在闭包中显式地引用self
}
}
var closureInstance = ClosureClass()
closureInstance.doSomethingAboutEscape()
print(closureInstance.x)
-closureHandler[0]()
+
+closureHandler[0]() // 两种调用逃逸闭包的方法
+closureHandler.first?()
print(closureInstance.x)
+// 自动闭包
printLine("AutoClosure")
print("Now Company Items:", company)
print("Company Item Count:", company.count)
// autoClosureHanlerA的type是 () -> String 不是 String
let autoClosureHandlerA = { company.remove(at: 0) } // an autoclosure lets you delay evaluation
-print("Company Item Count:", company.count)
+print("Company Item Count:", company.count) // 在自动闭包被调用前,值不会变
print("No Remove \(autoClosureHandlerA())")
print("Company Item Count:", company.count)
func autoClosureFuncParameterB(_ closure: @autoclosure () -> String) {
print("AutoClosureFuncParameterB \(closure())!")
}
+
+// 用@autoclosure修饰参数 可以在调用的时候少写一对`{}`
+// 过度使用@autoclosure会让代码变得难以理解
autoClosureFuncParameterB(company.remove(at: 0))
// @autoclosure 暗含了 noescape 特性
import UIKit
enum CompassPoint {
- case north
+ case north // 也可以写在一行上用逗号隔开 north, east, south, west
case east
case south
case west
}
-var directionToHead = CompassPoint.north
-directionToHead = .east
+var directionToHead = CompassPoint.north // 自动推断类型
+directionToHead = .east // 一旦类型确定,再次赋值时可以省略枚举类型名
switch directionToHead {
case .north: print("Losts of Plantes Have a North")
// Implicitly Assigned Raw Values
// 整型从0开始自增1,每个项的rawValue不能相同
enum Planet: Int {
- case mercury
+ case mercury // 从0开始,后续自增1,也可以指定值 如 mercury=1
case venus
case earth
case mars
case netpune
}
+
print(Planet.mercury, Planet.mercury.rawValue)
print(Planet.mars, Planet.mars.rawValue)
+
+// 使用原始值初始化枚举实例
+let possiblePlanet: Planet? = Planet(rawValue: 7) // 注意是可选类型 Planet?
+print(possiblePlanet!, possiblePlanet!.rawValue)
print(Planet(rawValue: 5)!, Planet(rawValue: 5)!.rawValue)
// String型,每项默认为枚举项的字符串值
enum CompassPointString: String {
- case North
+ case North // String原始值默认与枚举成员名相同
case East
case Sourth
- case West
+ case West = "WEAST"
}
print(CompassPointString.West, CompassPointString.West.rawValue)
+
+// 递归枚举
printLine("Resursive Enumerations")
// 不能忘记 indirect
indirect enum Exp {