Closures in Swift

1. Syntax 2. E.g of closure

What are the closure forms?

  • Global functions: They have a name and don’t capture any value
  • Nested functions: They have a name and can capture values from their enclosing function.
  • Closure expressions: They don’t have name and can capture values from their surrounding context.

Why you need closures?

  • It can be used as Completion Block: Closures help you to notify when some task finishes it’s execution
  • Higher order functions: In High order function instead of passing function as input it’s better to use closures. closures omits the func keyword and function name that makes the code more readable and short.

Closure Expressions:

  • Shorthand Argument Names
  • Implicit returns from single-expression closures
  • Trailing closure syntax
  • Inferring parameter and return value types from context

Shorthand Argument Names:

1. closure with one argument and output: 10 2. closure with two arguments and output: 10, Hello word

Implicit returns from single-expression closures:

output: 200 and 100

Trailing closure syntax:

Inferring parameter and return value types from context:

Capturing Values:

1. Closure are reference type. Even if you deallocate name object, closure hold the reference 2. Result :

Non-escaping Closures:

  1. Pass the closure as function argument
  2. Do some additional work with function.
  3. Function executes the closure
  4. Function returns the compiler back.
non-escaping closures

Escaping Closures:

  1. Pass the closure as function argument
  2. Do some additional work with function.
  3. Function execute the closure asynchronously or stored.
  4. Function returns the compiler back.
escaping closures

Autoclosures:

1. autoclosure and autoclosure with escaping 2. Result

Why closures are changed to non-escaping by default?

  • For better performance and code optimisation by the compiler and it’s safer.
  • A non-escaping closure can’t create a retain cycle.
  • To change closure to escaping use @escaping explicitly and create a weak capture list.

Errors with Closure:

  1. Change the closure to escaping using @escaping keyword

Closure Scope:

1. Closure didn’t capture the list(Example 1) 2. Closure capture the list(Example 2)

--

--

--

iOS Developer in walmart

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Concordium Mobile Wallet Updates

Data Modeling Using Enums in Swift Language

SwiftUI Tutorial: Applying Overlay to Image

Ripple into page with Flutter

10 Amazing New features released on iOS 14.3 — Update Now

SwiftUI | Email/Password Signup Using Firebase

Firebase and SwiftUI logo with an iPhone login page

SwiftUI in iOS 16: Using ShareLink for Sharing Content Like Text and Photos

Introduction to Swift Programming: Hello World Tutorial

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manasa M P

Manasa M P

iOS Developer in walmart

More from Medium

What is CollectionView ?

Swift Essentials| A Complete Guide to Swift Operations

Phantom Types in Swift

Static and Class Modifiers in Swift, iOS