- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 420字
- 2021-07-02 23:54:34
Capturing values
Closures can capture variables and constants from the surrounding context in which they are created. Closures can refer to these variables and modify them within their body, even if the original scope that defined variables no longer exists.
A closure is said to escape a function when the closure is passed as an argument to the function but is called after the function returns. One way that a closure can escape is by being stored in a variable that is defined outside the function.
The following is an example of escaping closures, in other words, completion handlers:
func sendRequest(completion: @escaping (_ response: String?, _ error: Error?) -> Void) {
// execute some time consuming operation, if successfull {
completion("Response", nil)
//}
}
sendRequest {
(response: String?, error: Error?) in
if let result = response {
print(result)
} else if let serverError = error {
// Error
}
}
We have a function named sendRequest that has a parameter of type of closure, completion which it takes an OptionalString, and an Optional Error parameters, and does not return any value.
Suppose that we execute some asynchronous time-consuming operations in the body of the function, such as reading from a file, reading from a database, or calling a web service.
To call this function, we provide a closure as argument. Our closure has two variables in it: a variable named response of the Optional String type and an error variable of the Optional Error type. As our function does not have any return type, it does not return any value to its caller. Here comes the concept of escaping a function.
Our passed closure escapes our function as it will be called after our time-consuming asynchronous operation finishes with success and the following call happens:
completion("Response", nil)
If a closure is going to be passed as an argument to a function and it is going to be invoked after the function returns, the closure is escaping. In other words, the closure argument escapes the function body. In Swift 3, we need to use the @escaping keyword in the function definition to tell our function users that closure can escape the function body.
In this call, we pass the response and error and call back the completion closure. Then the body of closure in the caller function is executed with passed variables. This concept is a very powerful concept that eases all asynchronous operations. It is very readable and easy to follow compared with mechanisms such as delegation and notification.
- GitHub Essentials
- Greenplum:從大數(shù)據(jù)戰(zhàn)略到實(shí)現(xiàn)
- 算法競(jìng)賽入門經(jīng)典:習(xí)題與解答
- 復(fù)雜性思考:復(fù)雜性科學(xué)和計(jì)算模型(原書第2版)
- 大數(shù)據(jù)時(shí)代下的智能轉(zhuǎn)型進(jìn)程精選(套裝共10冊(cè))
- INSTANT Cytoscape Complex Network Analysis How-to
- 達(dá)夢(mèng)數(shù)據(jù)庫(kù)性能優(yōu)化
- 數(shù)據(jù)架構(gòu)與商業(yè)智能
- 企業(yè)級(jí)數(shù)據(jù)與AI項(xiàng)目成功之道
- 數(shù)據(jù)庫(kù)原理與應(yīng)用
- 區(qū)域云計(jì)算和大數(shù)據(jù)產(chǎn)業(yè)發(fā)展:浙江樣板
- 爬蟲實(shí)戰(zhàn):從數(shù)據(jù)到產(chǎn)品
- 實(shí)現(xiàn)領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)
- 中國(guó)云存儲(chǔ)發(fā)展報(bào)告
- AndEngine for Android Game Development Cookbook