官术网_书友最值得收藏!

Closures

Closures are self-contained blocks of code that provide a specific functionality and can be stored, passed around, and used in the code. Closures are the equivalent of blocks in C and Objective-C. Closures can capture and store references to any constants and variables from the context in which they are defined. Nested functions are special cases of closures.

Closures are reference types that can be stored as variables, constants, and type aliases. They can be passed to and returned from functions.

The following examples present different declarations of closures in Swift from the website, http://goshdarnclosuresyntax.com:

// As a variable: 
var closureName: (parameterTypes) -> (returnType)

//As a type alias:
typealias closureType = (parameterTypes) -> (returnType)

//As an argument to a function call:
func Name({ (ParameterTypes) -> (ReturnType) in statements })

Closures and first-class, higher-order, and pure functions will be covered in detail in Chapter 2, Functions and Closures.

主站蜘蛛池模板: 武宣县| 阳江市| 万载县| 五家渠市| 米脂县| 正安县| 会泽县| 上饶市| 肃北| 武定县| 宜良县| 枞阳县| 孟村| 菏泽市| 内丘县| 通化县| 商洛市| 怀远县| 永靖县| 孟津县| 怀仁县| 荆门市| 永仁县| 南华县| 庆阳市| 香港 | 周至县| 泰和县| 基隆市| 防城港市| 尖扎县| 铜鼓县| 阳谷县| 通许县| 普兰店市| 资兴市| 钦州市| 兴山县| 怀宁县| 怀远县| 时尚|