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


相关内容

热门资讯

【营收拆解】博通 最新营收结构... 博通(AVGO)最新财报显示,公司本季度实现营业收入295.91亿美元。按业务划分:1. 产品收入2...
恶意对比拉踩!长安汽车发布声明 (来源:车联新生态)9月11日,长安汽车不实信息举报中心针对近日网传启源Q06发布会的不实信息发布声...
菲律宾海警:76人死亡,13人... 菲律宾客船起火事故死亡人数升至76人菲律宾海岸警卫队12日说,菲巴拉望省附近海域客船起火事故已造成7...
视频丨让 “看不见的服务” 被... 9月12日起,2026年服贸会迎来公众开放日。只需通过官网或公众号等平台提前预约,就可以亲临北京首钢...
三甲专家社区坐诊 七大专病门诊... 本报讯(大连新闻传媒集团/大连云记者张丽霞)“以后看眼睛,不用再往大医院跑了。”近日,在甘井子区泉水...