Welcome to Lesson 2 of Intro to Functional Swift. In the previous lesson, you've learned how to create a closure block with various forms. Today, you will learn how to create a special function. It not only takes a closure block as its parameter and but also returns it.
Can you pass/return a function/functions to a function?
Add two numbers using a closure
var addWithClosures: (Int, Int) -> Int = { (number1: Int, number2: Int) in
return number1 + number2
}
Return String
var returnString: () -> String = { () in
"hello"
}
let returnedValue = returnString() // "hello"
The Swift functions and closures are often referred as "First Class Citizen". You may
- store in a variable/constant
- pass as a parameter
- return
Disclosure: The Swift Function is called Global Closure
Let us create a function that returns a closure block. There are two ways.
First, design a function whose return type is (Int, Int) -> Int
.
func returnClosure() -> ((Int, Int) -> Int) {
return addWithClosures
}
let addClosure = returnClosure()
addClosure(4, 10)
returnClosure()(4, 10) // addWithClosure(4, 10)
You've returned addWithClosures
whose type is (Int, Int) -> Int
as well. If there is no ()
at the end, it is considered as a constant/variable.
You may return a closure block directly instead of addWithClosures
.
func returnClosureDirectly() -> ((Int, Int) -> Int) {
return { (number1, number2) in number1 + number2 }
}
returnClosureDirectly()(4, 10) // 14
You may use the short from instead.
func returnClosureDirectlyTwo() -> ((Int, Int) -> Int) {
return { $0 + $1 }
}
returnClosureDirectlyTwo()(4, 10) // 14
Create a function whose parameter accepts () -> String
. Then, execute the closure block within the function.
func insertClosureBlock(closureBlock: () -> String) -> String {
return closureBlock()
}
Create a closure which will be passed in the insertClosureBlock
function.
func hello() -> String {
return "hello"
}
insertClosureBlock(closureBlock: hello)
// "hello"
insertClosureBlock(closureBlock: { _ in return "hello" })
insertClosureBlock(closureBlock: { return "hello" })
insertClosureBlock(closureBlock: { "hello" })
No Fear Closures with Bob Part 1 and Part 2.
3002_intro_closures_part2.playground
You've learned how to pass and return a closure block directly and indirectly within a function. The is the fundamental root of functional programming which I'm not going to dive much into in this course. You may sign up for my mailing list if you are interested in learning about reactive programming with RxSwift and MVVM.
We've covered the basics of closures in Swift. From now on, difficulty of this chapter will dramatically increase. Before you step onto the arena, I recommend you to read my articles and practice on your own until you become well-versed with the closure syntax. If not, you will suffer.
In the following lesson, you will learn how to initialize an object using a closure block and the meaning of lazy
in the Swift Programming Language.
Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.