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


相关内容

热门资讯

财政部会同农业农村部、水利部下... 转自:新华财经新华财经北京8月3日电 据财政部网站3日消息,近日,财政部会同农业农村部、水利部下达农...
石四药集团(02005)投标共... 石四药集团(02005)发布公告,于最近中国第十二批全国药品集中采购招标中,集团投标共十三个品种十六...
伊朗处决两名间谍 转自:长安街知事据新华社消息,伊朗司法部门8月3日发布消息说,两名被控为以色列提供情报的间谍当天已被...
石四药集团(02005.HK)... 格隆汇8月3日丨石四药集团(02005.HK)公告,于最近中国第十二批全国药品集中采购招标中,集团投...
健盛集团上半年营收12.11亿... 8月3日,健盛集团发布2026年半年报。报告显示,公司上半年营业收入为12.11亿元,同比增长3.4...