Closures in Swift are similar to blocks in Objective-C.Closures are self-contained blocks of functionality that can be passed around and used in your code.Closures can capture and store references to any constants and variables from the context in which they are defined.
Closure Syntax:
{ (parameters) -> return type in
statements
}
We can pass in-out parameters, but they can’t have a default value. Variadic parameters can be used if you name the variadic parameter. Tuples can also be used as parameter types and return types.- Take a SingleView Application.Now create a new file and name it Test.swift
- completionHandler and also created a function named "getFullName". TestCompletionHandler is passed in that function as a parameter. This completion handler having two parameters.
Now I call comletionHandler() and passing firstName and lastName.
3)Now open your ViewController.swift
Completion Handler In Swift3.0
Steps1)- Now in viewDidLoad() method you can see that I have created the object of Test Class.After that I called my function name. I have used break point for debugging the values of firstname and lastname.
Here we can see that first having a value named "akshay".These values are passed during the completionHandler call.In last I have printed the full name.
Conclusion :
Today we learn about completion handler closure.Mostly we can use completion handler closure for network api calling.If you have any questions just leave a comment below and I’ll respond as soon as I can.
No comments:
Post a Comment