the MOST simplest way to write a closure or completion handler

I swear, this will be the most simple way to write a closure.

Lets consider the return type to be Void

Start with the input parameters


Next lets move on to the return type


Now lets combine the above statements

() -> ()

Let’s make use of this, as a completion handler

	func testCompletionHandlers(completion: (Int)->()) -> Void {

        testCompletionHandlers { number in

It’s that simple.

Now, lets use this logic when using a Map

let collection = { (elementOfCollection) -> ResultType in

To make it easier, the closure is very similar to the one we described above.

let users = [Info(firstName: "tom", lastName: "jones"), Info(firstName: "tdddom", lastName: "jak"), Info(firstName: "atom", lastName: "jones"), Info(firstName: "taom", lastName: "jones")]
            let siblings = { users in
	    return users.lastName
print("siblings \(siblings)")

You can use this logic to construct your completion handlers


Leave a Reply

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