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 {
completion(5)
}
testCompletionHandlers { number in
print("number")
}
It’s that simple.
Now, lets use this logic when using a Map
let collection = input.map { (elementOfCollection) -> ResultType in
return
}
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.map { users in
return users.lastName
}
print("siblings \(siblings)")
You can use this logic to construct your completion handlers
Leave a Reply