【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 搜索即可~


相关内容

热门资讯

博通6月3日财报:面临730亿... 博通将于6月3日公布第二财季财报,市场预计其业绩出色,普遍预期每股收益2.40美元,营收221亿美元...
工美博览·热卖现场|线上线下齐... (来源:消费日报官方平台)转自:消费日报官方平台5月30日,2026中国工艺美术博览会进入第二天。恰...
量子任务进展顺利:计算、通信与... 2023年4月启动的国家量子任务(NQM),8年总预算600.365亿卢比,已拨款400亿卢比,旨在...
马耳他提前举行议会选举 马耳他5月30日提前举行议会选举。多项民调显示,执政党工党有望连续第四次胜选。投票当天7时开始,将于...
基地建设提质增效 冷链物流跑出... 今年以来,超过10万吨榴莲、山竹等东南亚特色水果搭乘中老铁路冷链班列,从泰国、老挝出发经过磨憨站,再...