【Swift 60秒】53 - Using closures as parameters when they accept parameters
创始人
2024-02-08 20:59:28

0x00 Lesson

This is where closures can start to be read a bit like line noise: a closure you pass into a function can also accept its own parameters.

We’ve been using () -> Void to mean “accepts no parameters and returns nothing”, but you can go ahead and fill the () with the types of any parameters that your closure should accept.

To demonstrate this, we can write a travel() function that accepts a closure as its only parameter, and that closure in turn accepts a string:

func travel(action: (String) -> Void) {print("I'm getting ready to go.")action("London")print("I arrived!")
}

Now when we call travel() using trailing closure syntax. our closure code is required to accept a string:

travel { (place: String) inprint("I'm going to \(place) in my car")
}

0x01 我的小作品

欢迎体验我的作品之一:小汉字-XHanzi
汉字书写入门,常用汉字 3800 个,二级字表 2200
App Store 搜索即可~


相关内容

热门资讯

“现实中的龙餐馆”:他曾在伊拉... 编者的话:随着电影《欢迎来龙餐馆》的热映,影片主角“徐福”在中东某国经营中餐馆的传奇经历触动了许多观...
希腊萨拉米斯岛森林火灾已致2死... 新华财经雅典8月17日电(记者陈刚)希腊媒体16日援引该国消防部门消息报道,位于希腊首都雅典以西约2...
鸿路钢构伦交所ESG评级较上年... 8月17日消息,近期伦交所发布最新ESG评级结果,鸿路钢构获得C-级,相较于上一年的C级,评级下降。...
广汽集团伦交所ESG评级维持B... 8月17日消息,近期伦交所发布最新ESG评级结果,广汽集团获得B+级,相较于上一年的B+级,评级维持...
《大荒歌》再现一代垦荒人的命运...   发布会现场。本报记者 封娇摄  ■实习生 宋依轲   本报特派记者 封娇上海报道  8月15日,...