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


相关内容

热门资讯

第八届西洽会签约项目212个 正式合同额超1500亿元  据新华社重庆5月21日电(记者 赵佳乐 李晓婷)第八届中国西部国际投资贸...
数智川军亮相 展示硬核“科技范... 第二十二届“深圳文博会”开幕四川展馆总面积980平方米,汇聚了70余家本土文化企业、园区及基地。图据...
“泡药杨梅”的反思不能止步 惩戒虽已落下图为福建漳州的杨梅。图据新华社客户端  又是一年杨梅红。本该是果农丰收开心、消费者甜透心...
导演王可然:让观众在故事中看见... 央华戏剧新剧《她们》即将首演导演王可然 主办方供图  5月18日,央华戏剧2026年度最新力作——舞...
美军航母进入加勒比海 特朗普:...   5月20日是古巴独立日。当天,美军宣布“尼米兹”号航空母舰战斗群已进入加勒比海,随同“尼米兹”号...