Swift capture
-
Upload
chihyang-li -
Category
Software
-
view
68 -
download
0
Transcript of Swift capture
Swift Closures CaptureSai Li @ Swift Taipei.
2016/11/1
• Closures Capture Semanticsby Olivier Halligon
• Swift ( ) by John Lin @johnlinvc
• Memory Management by Barry
class Pokemon
delay()
delay()
Swift 3
demo1
demo1
demo1
demo1
demo1
demo1
demo1
demo1
demo1
demo1
demo1
Capture
Capture
Swift Capture 1.
captured variables are evaluated at the closure execution’s time
demo2()
Swift Capture 2.
Swift captures variables by reference by default
demo3()
demo3()
Swift Capture 3.
Swift use capture list to capture variables at closure creation
1. Pikachu created
1. Pikachu created2. Captured by closure as a copy
1. Pikachu created2. Captured by closure as a copy3. assign pokemon to Charmander,
Pikachu retained by closure
1. Pikachu created2. Captured by closure as a copy3. assign pokemon to Charmander,
Pikachu retained by closure4. leave demo6(), Charmander(pokemon) released
1. Pikachu created2. Captured by closure as a copy3. assign pokemon to Charmander,
Pikachu retained by closure4. leave demo6(), Charmander(pokemon) released5. closure execute, print “Pikachu”, capture by closure
1. Pikachu created2. Captured by closure as a copy3. assign pokemon to Charmander,
Pikachu retained by closure4. leave demo6(), Charmander(pokemon) released5. closure execute, print “Pikachu”, capture by closure6. closure released by GCD
1. Pikachu created
1. Pikachu created2. closure a reference to pokemon, not value(Pikachu)
1. Pikachu created2. closure a reference to pokemon, not value(Pikachu)3. pokemon was assigned to Charmander,
Pikachu was release(no strong reference)
1. Pikachu created2. closure a reference to pokemon, not value(Pikachu)3. pokemon was assigned to Charmander,
Pikachu was release(no strong reference)4. pokemon variable was referenced by the closure
1. Pikachu created2. closure a reference to pokemon, not value(Pikachu)3. pokemon was assigned to Charmander,
Pikachu was release(no strong reference)4. pokemon variable was referenced by the closure 5. pokemon release by GCD
Conclusion
Conclusion1. Capture a reference to the outer variables that
you use inside the closure
Conclusion1. Capture a reference to the outer variables that
you use inside the closure
2. Evaluated when the closure gets executed.
Conclusion1. Capture a reference to the outer variables that
you use inside the closure
2. Evaluated when the closure gets executed.
3. You can modify the variable’s value in the closure (if that variable is declared as var)
Conclusion1. Capture a reference to the outer variables that
you use inside the closure
2. Evaluated when the closure gets executed.
3. You can modify the variable’s value in the closure (if that variable is declared as var)
4. You can instead tell Swift to evaluate a variable at the point of the closure creation and store that value in a local constant, instead of capturing the variable itself. You do that using capture lists expressed inside brackets.
Q & A
One more thing
• iOS Developer
• Ops
• Backend
Recruitment
• iOS App
• Swift
• RESTful API
• Bonus
• Storyboard Auto-layout
• Git • Objective-C • CocoaPods or Carthage
iOS
• : • Linux • shell script python • VM Docker AWS • • CPU • • nginx nodejs mongodb • CI CD jenkins • • TDD
• • 1. • 2. • 3.
Ops
•
• 1. Server API (RESTful) (javascript)
• 2. (NoSQL)schema
• 4.Linux environment
• 5. Node.js + Express + MongoDB + mongoose
•
• javascript object oriented programming
• Linux
• node.js express.js mongoose, mongodb
• RESTful API
• JSON
• Git
Backend
• http://alisoftware.github.io/swift/closures/2016/07/25/closure-capture-1/
• Swift ( ) by johnlinvc
• http://img.gawkerassets.com/img/198dkaqba04b6png/original.png
• http://cartoonbros.com/wp-content/uploads/2016/08/pikachu-5.png
• http://vectorlogo4u.com/wp-content/uploads/2016/08/chramander-vector-high-res.png
• http://cdn.bulbagarden.net/upload/thumb/7/78/150Mewtwo.png/250px-150Mewtwo.png
• http://cdn.bulbagarden.net/upload/thumb/3/39/007Squirtle.png/250px-007Squirtle.png
References