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

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.

主站蜘蛛池模板: 南部县| 石嘴山市| 广饶县| 青河县| 平泉县| 河津市| 枝江市| 云安县| 大竹县| 昌都县| 共和县| 桃江县| 武定县| 徐水县| 虎林市| 克拉玛依市| 华容县| 永康市| 泊头市| 会理县| 东丰县| 澎湖县| 云浮市| 和林格尔县| 申扎县| 昌乐县| 布尔津县| 秭归县| 临泽县| 库伦旗| 高淳县| 五指山市| 淮南市| 安西县| 和林格尔县| 墨脱县| 兰坪| 婺源县| 天柱县| 贵南县| 三穗县|