Posts Boring guide to Closures in Swift - Introduction
Post
Cancel

Boring guide to Closures in Swift - Introduction

  • Closures are blocks of code like functions that can be passed around as variables to other functions to be used at a later point of time.

  • They can also be returned by functions.

For example in the following example processString takes a String as its first paramter and than takes a function as its second parameter. And that function itself takes a String as its parameter and does some processing on it. But the processString itself does not know what kind of processing as to be done. So the function passed to processString has the job the to the processing.

1
2
3
4
5
func processString(stringValue: String, process: (String) -> Void) {
    print("Processing for \(stringValue) is about to start")
    process(stringValue)
    print("Processing for \(stringValue) ended")
}

We can perform different kinds of processings on the string parameter according to what kind of process function we pass to processString method. Below we are defining 2 types of process method.

1
2
3
4
5
6
7
8
9
10
11
12
13
func process5Times(string: String) {
  for _ in 0..<5 {
      print("Slow Processing \(string)...")
      sleep(2)
  }
}

func process10Times(string: String) {
  for _ in 0..<10 {
      print("Fast Processing \(string) more times...")
      sleep(1)
  }
}

Now if we can decide how to the process the String passed as first parameter to processString method by passing one of the above method.

If we want to process 5 times, we just pass that method as the second parameter to processString method.

1
processString(stringValue: "This is large string to process", process: process5Times)

Output: It will print 5 times Slow Processing... every 2 seconds

1
2
3
4
5
6
7
Processing for This is large string to process is about to start
Slow Processing This is large string to process...
Slow Processing This is large string to process...
Slow Processing This is large string to process...
Slow Processing This is large string to process...
Slow Processing This is large string to process...
Processing for This is large string to process ended

And if we want to process 10 times, we just pass the process10Times as the second parameter.

1
processString(stringValue: "This is large string to process", process: process10Times)

Output: It will print 10 times Fast Processing more times... every 1 second

1
2
3
4
5
6
7
8
9
10
11
12
Processing for This is large string to process is about to start
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Fast Processing This is large string to process more times...
Processing for This is large string to process ended

So this is how function can be passed as parameters to other functions. One benefit of passing functions is, if you need to do something common before or after the processing, you do not need to it multiple times.

But this does not looks so easy to write. Everytime you have to create a function and pass it as parameter to another function. There should be something that is easier than this. And YES there is. And that is called Closure Expression syntax. We will learn about that in the next post.

This post is licensed under CC BY 4.0 by the author.

Contents

Trending Tags