]> Zhao Yanbai Git Server - acecode.git/commitdiff
closure
authorAceVest <zhaoyanbai@126.com>
Sun, 19 Jun 2016 15:44:43 +0000 (23:44 +0800)
committerAceVest <zhaoyanbai@126.com>
Sun, 19 Jun 2016 15:44:43 +0000 (23:44 +0800)
learn/AcePlay/AcePlay.playground/Pages/Closure.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate

index 66a43a80261e622fc4060afb40ef5f6d41d4c0c0..cd5b70d047ff751e1631095e942df297550e58e3 100644 (file)
@@ -24,7 +24,7 @@ import UIKit
 
  */
 
-let company = ["Tencent", "Apple", "Facebook", "Google", "Twitter", "Amazon"]
+var company = ["Tencent", "Apple", "Facebook", "Google", "Twitter", "Amazon"]
 var sortedCompany: [String] = []
 
 printLine("Sort")
@@ -120,11 +120,80 @@ func makeIncrementer(step:Int) -> () -> Int {
     return inc
 }
 
-var closureFuncA = makeIncrementer(1)
+// 闭包是引用类型
+let closureFuncA = makeIncrementer(1)
 print("ClosureFuncA:", closureFuncA())
 print("ClosureFuncA:", closureFuncA())
-var closureFuncB = closureFuncA
+let closureFuncB = closureFuncA
 print("ClosureFunnB:", closureFuncB())
-var closureFuncC = makeIncrementer(1)
+let closureFuncC = makeIncrementer(1)
 print("ClosureFuncC:", closureFuncC())
 
+
+// 逃逸&非逃逸闭包
+printLine("Noescaping & Escaping Closesure")
+func noescapingClosure(@noescape closure: () -> Void) {
+    closure()
+}
+
+var closureHandler: Array<() -> Void> = []
+func escapingClosure(closure: () -> Void) {  // 此时参数前加@noescape会报错
+    closureHandler.append(closure)
+}
+
+
+class ClosureClass {
+    var x = 10
+    func doSomethingAboutEscape() {
+        noescapingClosure() { x = 200 }     // 将参数标记为@noescape能在闭包中隐式地引用self
+        escapingClosure() { self.x = 100 }
+    }
+}
+
+var closureInstance = ClosureClass()
+closureInstance.doSomethingAboutEscape()
+print(closureInstance.x)
+closureHandler[0]()
+print(closureInstance.x)
+
+
+printLine("AutoClosure")
+print("Now Company Items:", company)
+print("Company Item Count:", company.count)
+// autoClosureHanlerA的type是 () -> String 不是 String
+let autoClosureHandlerA = { company.removeAtIndex(0) }  // an autoclosure lets you delay evaluation
+print("Company Item Count:", company.count)
+print("No Remove \(autoClosureHandlerA())")
+print("Company Item Count:", company.count)
+
+
+// autoclosure parameter
+printLine("AutoClosure Parameter")
+func autoClosureFuncParameterA(closure: () -> String) {
+    print("AutoClosureFuncParameterA \(closure())!")
+}
+autoClosureFuncParameterA({ company.removeAtIndex(0) })
+
+func autoClosureFuncParameterB(@autoclosure closure: () -> String) {
+    print("AutoClosureFuncParameterB \(closure())!")
+}
+autoClosureFuncParameterB(company.removeAtIndex(0))
+
+// @autoclosure 暗含了 noescape 特性
+var autoClosureHanlder: [() -> String] = []
+func autoClosureFuncParameterC(@autoclosure closure: () -> String) {
+    //因为参数被@autoclosure修饰了,而@autoclosure暗含@noescape特性,因此以下语句会报错
+    //autoClosureHanlder.append(closure)
+}
+
+// 如果用了@autoclosure又要用escape特性,则用@autoclosure(escaping)修饰参数
+func autoClosureFuncParameterD(@autoclosure(escaping) closure: () ->String) {
+    print("Called autoClosureFuncParameterD")
+    autoClosureHanlder.append(closure)
+}
+autoClosureFuncParameterD(company.removeAtIndex(0))
+autoClosureFuncParameterD(company.removeAtIndex(0))
+
+for handler in autoClosureHanlder {
+    print("autoClosure Handling \(handler())!")
+}
index 5f08ca2a96df821454713de6a53f5ff0e5314444..1bafc59b124ca6ef98ca7707abd10100e270d426 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