]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authoracevest <zhaoyanbai@126.com>
Wed, 8 Mar 2017 05:29:46 +0000 (13:29 +0800)
committeracevest <zhaoyanbai@126.com>
Wed, 8 Mar 2017 05:29:46 +0000 (13:29 +0800)
learn/AcePlay/AcePlay.playground/Pages/Closure.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Sources/Utils.swift
learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate

index 6957e7eaf38db065aa30a05e9ac3b15277b75050..af95109f00aa3cbb9aa658a6c7bf058128dc0663 100644 (file)
@@ -63,6 +63,22 @@ sortedCompany = company.sorted(by: { $0 > $1 })
 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:
  */
@@ -121,7 +137,7 @@ func makeIncrementer(_ step:Int) -> () -> Int {
 }
 
 // 闭包是引用类型
-let closureFuncA = makeIncrementer(1)
+let closureFuncA = makeIncrementer(2)
 print("ClosureFuncA:", closureFuncA())
 print("ClosureFuncA:", closureFuncA())
 let closureFuncB = closureFuncA
@@ -131,13 +147,16 @@ print("ClosureFuncC:", closureFuncC())
 
 
 // 逃逸&非逃逸闭包
+// 当一个闭包作为参数传到一个函数中,但是这个闭包在函数返回之后才被执行,则称该闭包从函数中逃逸
+// 可以在定义参数时,在参数名前标注@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)
 }
 
@@ -146,23 +165,26 @@ class ClosureClass {
     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)
 
@@ -177,6 +199,9 @@ autoClosureFuncParameterA({ company.remove(at: 0) })
 func autoClosureFuncParameterB(_ closure: @autoclosure () -> String) {
     print("AutoClosureFuncParameterB \(closure())!")
 }
+
+// 用@autoclosure修饰参数 可以在调用的时候少写一对`{}`
+// 过度使用@autoclosure会让代码变得难以理解
 autoClosureFuncParameterB(company.remove(at: 0))
 
 // @autoclosure 暗含了 noescape 特性
index 3ce322cd854003c70de6be5984b583f9fe4382a8..4510d24746f8ac06edd769c876f3cbdfa2cbbf65 100644 (file)
@@ -3,14 +3,14 @@
 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")
@@ -49,7 +49,7 @@ enum ASCIIControlCharacter: Character {
 // Implicitly Assigned Raw Values
 // 整型从0开始自增1,每个项的rawValue不能相同
 enum Planet: Int {
-    case mercury
+    case mercury    // 从0开始,后续自增1,也可以指定值 如 mercury=1
     case venus
     case earth
     case mars
@@ -59,17 +59,22 @@ enum Planet: Int {
     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"
 }
 
 
@@ -79,6 +84,8 @@ print(CompassPointString.Sourth,CompassPointString.Sourth.rawValue)
 print(CompassPointString.West,  CompassPointString.West.rawValue)
 
 
+
+// 递归枚举
 printLine("Resursive Enumerations")
 // 不能忘记 indirect
 indirect enum Exp {
index 8c6499ae882e8eb97e2bc1d2eb7c3d4afcb29323..17f718579c57776b1ec99003167d60094ff1c7bd 100644 (file)
@@ -2,6 +2,6 @@ import UIKit
 
 public
 func printLine(_ title: String) -> Void {
-    let line = String(format:"[%@]-----------------------------------", title)
+    let line = String(format:"-----------------------------------<%@>", title)
     print(line)
 }
index fa0d0147ea01c7f18ee998865ac38ad45643b67f..a5bd0eda41a23e5c276d7a45825adfa46ff6f58d 100644 (file)
Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ