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


相关内容

热门资讯

华灿光电副董事长及非独立董事谢... 3月23日,华灿光电公告,因工作调动,谢浩先生不再担任副董事长及非独立董事职务。资料显示,谢浩先生:...
线下消费爆发 2月备付金规模仍... (来源:北京商报)北京商报讯(记者 刘四红)最新一期支付机构备付金数据披露。3月22日,北京商报记者...
伊朗:若发电站遭袭将全面关闭霍...   据伊朗伊斯兰共和国广播电视台22日报道,伊朗武装部队当天发表声明表示,如果美国总统特朗普袭击伊朗...
LPR连续10月持稳 年中降息... (来源:北京商报)3月20日,LPR报价如期出炉。中国人民银行授权全国银行间同业拆借中心公布,202...
三安光电实际控制人被留置 (来源:五道口江湖)三安光电3月22日公告,公司于3月21日收到福建三安集团有限公司通知,福建三安集...