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


相关内容

热门资讯

靖洋集团(08257):上市地... 靖洋集团(08257)发布公告,公司已根据GEM上市规则第9.23(2)条向联交所申请,而联交所已批...
阳气不足适合喝咖啡 口干舌燥慎... 来源:@央视财经微博 【#阳气不足适合喝咖啡# #口干舌...
利好,603103,30025...   即将披露2025年年报的业绩暴增股来了。  据界面新闻报道,2月21日,猫眼专业版数据显示,20...
网络中国节·春节丨科技馆里过大... 2026年马年春节,西安文旅市场持续火爆。春晚的人形机器人舞动全国,也在西安掀起了一股以探索和体验为...
天泓文创(08500):刘立汉... 天泓文创(08500)发布公告,梁薇女士(梁女士)不再担任香港联合交易所有限公司(联交所) GEM证...