Completion handler in iOS swift 5

Completion handler in iOS swift 5
April 24, 2020 No Comments Development,iOS Development Pushpendra Kumar

In this tutorial, I will explain to you what is the completion handler and how you can use the completion handler in your iOS project. And in this tutorial, I have also explained to you what is a difference between the completionHandler and return statement. All these things I am trying to clear with the help of a basic example. For more details please follow the complete tutorial.

Completion handler in iOS swift 5

What is the that? The completion handler is a technique to return data after completing the task of the function. It is a practical answer. And I hope you like it. Seems like you are quite confused now. But you will understand this completely after following this complete tutorial. After that, we will get to the point directly. And we will know how to integrate the it in iOS with swift 5 programming language. And how it is differ from the return statement.

Firstly, We have a fresh project. And inside fresh project please create the following function.

    func handler(message : String, completion : (_ result : String)->Void) {
        print("Enter into the function")
        print("MESSAGE : \(message)")
        completion("This is the call of completion handler")
    }

In short, You have create a function where we are using completion handler. 

Use

Firstly, Let’s see about the function. The above function will take Parameters as string and in coipition handler also will take string parameter. After that, Call above function from the viewDidLoad() method.

    override func viewDidLoad() {
        super.viewDidLoad()
        handler(message: "Hi Pushpendra, I am calling function") { (result) in
            //MARK: - you will be navigate here with RESULT after completiong the work of handler function
            print("RESULT : \(result)")
        }
    }

Wonderful, Now Run ▶️ your project and see the output. The output is below.

Output

Enter into the function
MESSAGE : Hi Pushpendra, I am calling function
RESULT : This is the call of completion handler

So this the output, Now add one more line in your handler(message..) function after completion(….) as given below.

    func handler(message : String, completion : (_ result : String)->Void) {
        print("Enter into the function")
        print("MESSAGE : \(message)")
        completion("This is the call of completion handler")
        print("Executing after completion as well...")
    }

Here you will see that, It can execute after completion statement as well. So below is the output. 

Enter into the function
MESSAGE : Hi Pushpendra, I am calling function
RESULT : This is the call of completion handler
Executing after completion as well…

Create work…. So think you get some clue about the difference in between completionHandler and return statement. But, In addition I am saying it in very clear way.

In Completion Handler, The code can be executed which you will write after the completion statement in any function. whereas in return state code can not be executed which you will write after the return statement.

Difference in completion handler and return statement.

In addition, I will show you with the help of example. Which is below.

    func handlerWithReturn(message : String) -> String {
        print("Enetr into the function")
        print("MESSAGE : \(message)")
        return "This is the print after calling complition handler"
        print("**This line will not be execute **")
    }

Create guys, After that you can call this function from the viewDidLoad() function. And you can find the difference by yourself. 

In conclusion, I hope it’s clear now. But if still you are confuse then you must visit on the video which is relate to same question. Completion handler in iOS swift 5. And also subscribe my YouTube channel for the latest technology based tutorial for iOS. And if you want more interesting tutorial then follow the below tutorial list.

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *