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

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.

主站蜘蛛池模板: 潮州市| 瑞丽市| 长治市| 二连浩特市| 双流县| 绥滨县| 卢湾区| 东莞市| 桦南县| 舟曲县| 旌德县| 奎屯市| 梅州市| 阳城县| 米易县| 达孜县| 安新县| 东莞市| 开封县| 泰来县| 尤溪县| 台湾省| 泰安市| 恩施市| 定兴县| 清水河县| 鄯善县| 唐山市| 莆田市| 灌云县| 鄂托克前旗| 通山县| 十堰市| 鱼台县| 广东省| 奉新县| 内黄县| 波密县| 西林县| 旺苍县| 孝昌县|