在swift的官方文檔中荤堪,關(guān)于結(jié)構(gòu)體closure給出這樣一段描述:
Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it is executed—you saw an example of this already with nested functions. You can write a closure without a name by surrounding code with braces ({}). Use?in?to separate the arguments and return type from the body.
翻譯整理一下:
1.函數(shù)其實(shí)是一種特殊的機(jī)構(gòu)體:一段可以在之后相應(yīng)的代碼塊昂芜。
2.結(jié)構(gòu)體創(chuàng)建的時(shí)候參數(shù)可以是值或者函數(shù)课舍,甚至是另一個(gè)結(jié)構(gòu)體坛猪。
3.給出了結(jié)構(gòu)體的創(chuàng)建方法蛙紫,既用大括號(hào){}覆蓋,然后用in關(guān)鍵字將參數(shù)和返回值分開荷辕。
下面是官方文檔給出的例子: