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

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.

主站蜘蛛池模板: 长子县| 华蓥市| 左贡县| 泸西县| 年辖:市辖区| 门头沟区| 珲春市| 洛南县| 丹东市| 黑龙江省| 云安县| 板桥市| 广德县| 自治县| 鲁甸县| 台东县| 武平县| 卢氏县| 桐柏县| 寿光市| 即墨市| 苍梧县| 汕头市| 长岛县| 米泉市| 陆川县| 万荣县| 宁夏| 凉山| 盐源县| 海南省| 象山县| 阿拉善盟| 深州市| 墨竹工卡县| 图木舒克市| 博湖县| 鄯善县| 垦利县| 靖西县| 大同县|