what is Swift - Closures?
Syntax Following is a generic syntax to define closure which accepts parameters and returns a data type − { (parameters) −> return type in statements } Following is a simple example − let studname = { print ( "Welcome to Swift Closures" ) } studname () When we run the above program using playground, we get the following result − Welcome to Swift Closures The following closure accepts two parameters and returns a Bool value − { (Int, Int) −> Bool in Statement1 Statement 2 --- Statement n } Following is a simple example − let divide = { ( val1 : Int , val2 : Int ) -> Int in return val1 / val2 } let result = divide ( 200 , 20 ) print ( result ) When we run the above program using playground, we get the following result − 10 Expressions in Closures Nested functions provide a convenient way of naming and defining blocks of code. Instead of representing the whole function declaration and name constructs are used to ...