際際滷

際際滷Share a Scribd company logo
Swift Closures Capture
Sai Li @ Swift Taipei.
2016/11/1
 Closures Capture Semantics
by Olivier Halligon
 Swift ( ) 
by John Lin @johnlinvc
 Memory Management 
by Barry
Swift capture
Swift capture
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 executions time
demo2()
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift Capture 2.
Swift captures variables by reference
by default
demo3()
demo3()
Swift capture
Swift capture
Swift Capture 3.
Swift use capture list to capture variables
at closure creation
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
1. Pikachu created
1. Pikachu created
2. Captured by closure as a copy
1. Pikachu created
2. Captured by closure as a copy
3. assign pokemon to Charmander, 
Pikachu retained by closure
1. Pikachu created
2. Captured by closure as a copy
3. assign pokemon to Charmander, 
Pikachu retained by closure
4. leave demo6(), Charmander(pokemon) released
1. Pikachu created
2. Captured by closure as a copy
3. assign pokemon to Charmander, 
Pikachu retained by closure
4. leave demo6(), Charmander(pokemon) released
5. closure execute, print Pikachu, capture by closure
1. Pikachu created
2. Captured by closure as a copy
3. assign pokemon to Charmander, 
Pikachu retained by closure
4. leave demo6(), Charmander(pokemon) released
5. closure execute, print Pikachu, capture by closure
6. closure released by GCD
Swift capture
1. Pikachu created
1. Pikachu created
2. closure a reference to pokemon, not value(Pikachu)
1. Pikachu created
2. closure a reference to pokemon, not value(Pikachu)
3. pokemon was assigned to Charmander, 
Pikachu was release(no strong reference)
1. Pikachu created
2. 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 created
2. 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
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Swift capture
Conclusion
Conclusion
1. Capture a reference to the outer variables that
you use inside the closure
Conclusion
1. Capture a reference to the outer variables that
you use inside the closure
2. Evaluated when the closure gets executed.
Conclusion
1. 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 variables value in the
closure (if that variable is declared as var)
Conclusion
1. 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 variables 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
Swift capture
Swift capture
 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
Ad

Recommended

Ruby Presentation - Handout
Ruby Presentation - Handout
Christopher Giroir
What is while loop?
What is while loop?
AnuragSrivastava272
Flutter 2
Flutter 2
Warren Lin
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift
Jason Larsen
Punctuality In Schools
Punctuality In Schools
superuser99
Swift Study #3
Swift Study #3
chanju Jeon
Swift - One step forward from Obj-C
Swift - One step forward from Obj-C
Nissan Tsafrir
Go Programming Language (Golang)
Go Programming Language (Golang)
Ishin Vin
Image Style Transfer and AI on iOS Mobile App
Image Style Transfer and AI on iOS Mobile App
Chihyang Li
Image style transfer and iOS CoreML, Vision Frameworks
Image style transfer and iOS CoreML, Vision Frameworks
Chihyang Li
Machine Learning in Swift
Machine Learning in Swift
Chihyang Li
Image style transfer & AI on App
Image style transfer & AI on App
Chihyang Li
iOS Vision framework
iOS Vision framework
Chihyang Li
Design Patterns in Swift Ch5 Abstract Factory Builder
Design Patterns in Swift Ch5 Abstract Factory Builder
Chihyang Li
Design patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template Method
Chihyang Li
Swift map & flatMap
Swift map & flatMap
Chihyang Li
Swift Optional ??
Swift Optional ??
Chihyang Li
Design Pattern in Swift Ch2 Strategy, Decorator
Design Pattern in Swift Ch2 Strategy, Decorator
Chihyang Li
Design Pattern Ch3 in Swift Composite
Design Pattern Ch3 in Swift Composite
Chihyang Li
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Chihyang Li
Scrum & Kanban Introduction
Scrum & Kanban Introduction
Chihyang Li
Design Patterns in Swift ch0 Introduction
Design Patterns in Swift ch0 Introduction
Chihyang Li
Object Oriented Programming Ch3 SRP, DIP, ISP
Object Oriented Programming Ch3 SRP, DIP, ISP
Chihyang Li
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Chihyang Li
Object Oriented Programming in Swift Ch1 - Inheritance
Object Oriented Programming in Swift Ch1 - Inheritance
Chihyang Li
Object Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - Encapsulation
Chihyang Li
Apple Swift API Design Guideline
Apple Swift API Design Guideline
Chihyang Li
DevOps 2016 summit
DevOps 2016 summit
Chihyang Li
Sap basis role in public cloud in s/4hana.pptx
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3

More Related Content

More from Chihyang Li (20)

Image Style Transfer and AI on iOS Mobile App
Image Style Transfer and AI on iOS Mobile App
Chihyang Li
Image style transfer and iOS CoreML, Vision Frameworks
Image style transfer and iOS CoreML, Vision Frameworks
Chihyang Li
Machine Learning in Swift
Machine Learning in Swift
Chihyang Li
Image style transfer & AI on App
Image style transfer & AI on App
Chihyang Li
iOS Vision framework
iOS Vision framework
Chihyang Li
Design Patterns in Swift Ch5 Abstract Factory Builder
Design Patterns in Swift Ch5 Abstract Factory Builder
Chihyang Li
Design patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template Method
Chihyang Li
Swift map & flatMap
Swift map & flatMap
Chihyang Li
Swift Optional ??
Swift Optional ??
Chihyang Li
Design Pattern in Swift Ch2 Strategy, Decorator
Design Pattern in Swift Ch2 Strategy, Decorator
Chihyang Li
Design Pattern Ch3 in Swift Composite
Design Pattern Ch3 in Swift Composite
Chihyang Li
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Chihyang Li
Scrum & Kanban Introduction
Scrum & Kanban Introduction
Chihyang Li
Design Patterns in Swift ch0 Introduction
Design Patterns in Swift ch0 Introduction
Chihyang Li
Object Oriented Programming Ch3 SRP, DIP, ISP
Object Oriented Programming Ch3 SRP, DIP, ISP
Chihyang Li
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Chihyang Li
Object Oriented Programming in Swift Ch1 - Inheritance
Object Oriented Programming in Swift Ch1 - Inheritance
Chihyang Li
Object Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - Encapsulation
Chihyang Li
Apple Swift API Design Guideline
Apple Swift API Design Guideline
Chihyang Li
DevOps 2016 summit
DevOps 2016 summit
Chihyang Li
Image Style Transfer and AI on iOS Mobile App
Image Style Transfer and AI on iOS Mobile App
Chihyang Li
Image style transfer and iOS CoreML, Vision Frameworks
Image style transfer and iOS CoreML, Vision Frameworks
Chihyang Li
Machine Learning in Swift
Machine Learning in Swift
Chihyang Li
Image style transfer & AI on App
Image style transfer & AI on App
Chihyang Li
iOS Vision framework
iOS Vision framework
Chihyang Li
Design Patterns in Swift Ch5 Abstract Factory Builder
Design Patterns in Swift Ch5 Abstract Factory Builder
Chihyang Li
Design patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template Method
Chihyang Li
Swift map & flatMap
Swift map & flatMap
Chihyang Li
Swift Optional ??
Swift Optional ??
Chihyang Li
Design Pattern in Swift Ch2 Strategy, Decorator
Design Pattern in Swift Ch2 Strategy, Decorator
Chihyang Li
Design Pattern Ch3 in Swift Composite
Design Pattern Ch3 in Swift Composite
Chihyang Li
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Chihyang Li
Scrum & Kanban Introduction
Scrum & Kanban Introduction
Chihyang Li
Design Patterns in Swift ch0 Introduction
Design Patterns in Swift ch0 Introduction
Chihyang Li
Object Oriented Programming Ch3 SRP, DIP, ISP
Object Oriented Programming Ch3 SRP, DIP, ISP
Chihyang Li
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Chihyang Li
Object Oriented Programming in Swift Ch1 - Inheritance
Object Oriented Programming in Swift Ch1 - Inheritance
Chihyang Li
Object Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - Encapsulation
Chihyang Li
Apple Swift API Design Guideline
Apple Swift API Design Guideline
Chihyang Li
DevOps 2016 summit
DevOps 2016 summit
Chihyang Li

Recently uploaded (20)

Sap basis role in public cloud in s/4hana.pptx
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
Which Hiring Management Tools Offer the Best ROI?
Which Hiring Management Tools Offer the Best ROI?
HireME
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
From Data Preparation to Inference: How Alluxio Speeds Up AI
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
Top Time Tracking Solutions for Accountants
Top Time Tracking Solutions for Accountants
oliviareed320
Best Software Development at Best Prices
Best Software Development at Best Prices
softechies7
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
Sap basis role in public cloud in s/4hana.pptx
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
Which Hiring Management Tools Offer the Best ROI?
Which Hiring Management Tools Offer the Best ROI?
HireME
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
From Data Preparation to Inference: How Alluxio Speeds Up AI
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
Top Time Tracking Solutions for Accountants
Top Time Tracking Solutions for Accountants
oliviareed320
Best Software Development at Best Prices
Best Software Development at Best Prices
softechies7
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
Ad

Swift capture

  • 1. Swift Closures Capture Sai Li @ Swift Taipei. 2016/11/1
  • 2. Closures Capture Semantics by Olivier Halligon Swift ( ) by John Lin @johnlinvc Memory Management by Barry
  • 10. demo1
  • 11. demo1
  • 12. demo1
  • 13. demo1
  • 14. demo1
  • 15. demo1
  • 16. demo1
  • 17. demo1
  • 18. demo1
  • 21. Swift Capture 1. captured variables are evaluated at the closure executions time
  • 37. Swift Capture 2. Swift captures variables by reference by default
  • 42. Swift Capture 3. Swift use capture list to capture variables at closure creation
  • 61. 1. Pikachu created 2. Captured by closure as a copy
  • 62. 1. Pikachu created 2. Captured by closure as a copy 3. assign pokemon to Charmander, Pikachu retained by closure
  • 63. 1. Pikachu created 2. Captured by closure as a copy 3. assign pokemon to Charmander, Pikachu retained by closure 4. leave demo6(), Charmander(pokemon) released
  • 64. 1. Pikachu created 2. Captured by closure as a copy 3. assign pokemon to Charmander, Pikachu retained by closure 4. leave demo6(), Charmander(pokemon) released 5. closure execute, print Pikachu, capture by closure
  • 65. 1. Pikachu created 2. Captured by closure as a copy 3. assign pokemon to Charmander, Pikachu retained by closure 4. leave demo6(), Charmander(pokemon) released 5. closure execute, print Pikachu, capture by closure 6. closure released by GCD
  • 68. 1. Pikachu created 2. closure a reference to pokemon, not value(Pikachu)
  • 69. 1. Pikachu created 2. closure a reference to pokemon, not value(Pikachu) 3. pokemon was assigned to Charmander, Pikachu was release(no strong reference)
  • 70. 1. Pikachu created 2. 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
  • 71. 1. Pikachu created 2. 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
  • 111. Conclusion 1. Capture a reference to the outer variables that you use inside the closure
  • 112. Conclusion 1. Capture a reference to the outer variables that you use inside the closure 2. Evaluated when the closure gets executed.
  • 113. Conclusion 1. 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 variables value in the closure (if that variable is declared as var)
  • 114. Conclusion 1. 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 variables 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.
  • 115. Q & A
  • 119. iOS Developer Ops Backend Recruitment
  • 120. iOS App Swift RESTful API Bonus Storyboard Auto-layout Git Objective-C CocoaPods or Carthage iOS
  • 121. : Linux shell script python VM Docker AWS CPU nginx nodejs mongodb CI CD jenkins TDD 1. 2. 3. Ops
  • 122. 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
  • 123. 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