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


相关内容

热门资讯

*ST华幸:一季度亏损超17亿... (来源:财闻) 公司股票存在终止上市风险。 5月29...
5月锡铅金属行情复盘 六月... 5 月 29 日紧急复盘:美伊战火、美联储换帅、中国减持美债、锡矿断供... 五大 "黑天鹅" 同时...
景区“圈路收费”一再上演,别让... 继4月份被文旅部点名通报后,稻城亚丁景区又一次陷入舆论漩涡。近日,一名自驾博主发视频质疑,稻城亚丁景...
技源集团股东会审议通过多项议案... 2026年5月29日,技源集团股份有限公司在上海市徐汇区钦州北路1089弄54号楼5楼公司会议室召开...
“美军至少打死199人” 美国打击“运毒船”已致至少199人死亡自去年9月初以来,美军在加勒比海和东太平洋持续打击所谓“运毒船...