关于Swift语言中Closure的简化推导及简单使用

《Swift_Apprentice》是一本非常不错的swift入门书籍。
closure语法对刚入门的时候可能有点难以理解,再加上swift语言的特性,closure可以写得非常简洁,这给新同学造成了暂时的困难,下面我们一起看看在这本书中,它是怎么一步步递进推导closure并帮助我们理解的。

Shorthand syntax
Compared to functions, closures are designed to be lightweight. There are many ways to shorten their syntax. First, if the closure consists of a single return statement, you can leave out the return keyword, like so:
单一返回声明的时候可以忽略return关键字

multiplyClosure = { (a: Int, b: Int) -> Int in
a*b }

Next, you can use Swift’s type inference to shorten the syntax even more by removing the type information:
利用类型推导精简语法

multiplyClosure = { (a, b) in
a*b }

Remember, you already declared multiplyClosure as a closure taking two Ints and returning an Int, so you can let Swift infer these types for you.
And finally, you can even omit the parameter list if you want. Swift lets you refer to each parameter by number, starting at zero, like so:

multiplyClosure = {
$0 * $1 }

The parameter list, return type and in keyword are all gone, and your new closure declaration is much shorter than the original. Numbered parameters like this should really only be used when the closure is short and sweet, like the one above. If the parameter list is much longer it can be confusing to remember what each numbered parameter refers to. In these cases you should use the named syntax.

Consider the following code:

func operateOnNumbers(_ a: Int, _ b: Int,
                      operation: (Int, Int) -> Int) -> Int {
  let result = operation(a, b)
  print(result)
  return result
}

This declares a function named operateOnNumbers, which takes Int values as its first two parameters. The third parameter is named operation and is of a function type. operateOnNumbers itself returns an Int.
You can then use operateOnNumbers with a closure, like so:

let addClosure = { (a: Int, b: Int) in
a+b }
operateOnNumbers(4, 2, operation: addClosure)

Remember, closures are simply functions without names. So you shouldn’t be surprised to learn that you can also pass in a function as the third parameter of operateOnNumbers, like so:

func addFunction(_ a: Int, _ b: Int) -> Int {
return a + b }
operateOnNumbers(4, 2, operation: addFunction)

operateOnNumbers is called the same way, whether the operation is a function or a closure.
The power of the closure syntax comes in handy again: You can define the closure inline with the operateOnNumbers function call, like this:

operateOnNumbers(4, 2, operation: { (a: Int, b: Int) -> Int in
  return a + b
})

There’s no need to define the closure and assign it to a local variable or constant; you can simply declare the closure right where you pass it into the function as a parameter!
But recall that you can simplify the closure syntax to remove a lot of the boilerplate code. You can therefore reduce the above to the following:

operateOnNumbers(4, 2, operation: { $0 + $1 })

In fact, you can even go a step further. The + operator is just a function that takes
two arguments and returns one result so you can write:

  operateOnNumbers(4, 2, operation: +)

There’s one more way you can simplify the syntax, but it can only be done when the closure is the final parameter passed to a function. In this case, you can move the closure outside of the function call:

operateOnNumbers(4, 2) {
$0 + $1 }

This may look strange, but it’s just the same as the previous code snippet, except you’ve removed the operation label and pulled the braces outside of the function call parameter list. This is called trailing closure syntax.

OK,能看得懂最好,看不懂就看下面的我自己的经验例子好了。纯手打,如果有手误请指出。

//声明一个closure属性:
//()中没有内容,closure没有参数,不需要写Void,但返回值如果没有必须写Void

var testClosure: (()->Void)?

//在函数中传递一个closure作为参数:
//String是closure中参数的类型

func pass(closure: ((String)->Void))

//如果很多常用的,又不想重复写,可以用typealias关键字对类型进行重命名,用起来也很方便,比如上面的testClosure:

typealias EmptyClosure: (()->Void)

声明就变成这样了:

var testClosure: EmptyClosure

使用闭包的示范:

pass(closure:{ _ in
  ///如果不需要使用closure中的形参,可以用"_"进行忽略
})
pass(closure:{ text in
  /// text是自己起的名字,随意起。
  print(text)
})
pass(closure:{
  /// 如果懒得起名字,但想用这个参数中的值,可以用$0这种来代替,$0代表闭包中第一个形参。$1代表第二个,以此类推。但注意闭包中的in关键字已经没有写出来了,通过编译器隐式推导这是一个闭包。
  print($0)
})

如果调用的时候有产生循环引用,比如closure和self产生循环引用,通过类似oc里面的weak修饰,调用的时候如下:

pass(closure: {[weak self] text in 

  //前面的问号代表self有可能不存在了,后面的两个问号是对设置的默认值。
  print(self?.description ?? "place holder")
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,245评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,749评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,960评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,575评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,668评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,670评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,664评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,422评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,864评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,178评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,340评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,015评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,646评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,265评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,494评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,261评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,206评论 2 352