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


相关内容

热门资讯

言短意长|“烹饪大师”退场,餐... 地方行业协会的“大师”清理仍在进行时。近日,四川泸州市餐饮行业协会、泸州市餐饮行业工会联合会发出一纸...
国家发展改革委向民营企业 公开... (来源:嘉兴日报)转自:嘉兴日报  新华社北京9月11日电(记者 魏玉坤)记者11日从国家发展改革委...
弓弦之上 热爱与梦想同箭齐发 (来源:嘉兴日报)转自:嘉兴日报  ■记者 龚思雨  拉弓、瞄准、放弦,箭矢破空而出,一声清脆的“b...
漫画的启示 (来源:南湖晚报)转自:南湖晚报   长安镇中心小学605班 潘奕桐 指导老师 杜盈  漫画虽然不会...
三友联众聘任44岁杜欣洋为核心... 9月12日,三友联众公告,聘任杜欣洋先生为公司核心技术人员。资料显示,杜欣洋先生,中国国籍,无境外永...