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


相关内容

热门资讯

围绕“提升入境游便利化国际化水... 转自:全国政协5月19日至22日,全国政协副主席、农工党中央常务副主席杨震率全国政协调研组赴重庆市,...
江苏省电子商务职业教育座谈会暨... 2026年5月22日,江苏省电子商务职业教育座谈会暨电子商务服务实体经济研讨会在南京钟山宾馆(江苏省...
本周(5.16-5.22),武... 召开防汛救灾调度会议,开展树立和践行正确政绩观学习教育专题研讨,召开全市领导干部警示教育会,召开市委...
患者福音!无锡这家大医院发力细... 细胞治疗是精准医疗的前沿领域正在为诸多疑难杂症开辟新的治疗路径记者了解到由江南大学附属医院牵头推进的...
猜对有奖!“民法典宣传月”谐音... “民法典宣传月”谐音梗看图猜词特别版重磅上线!没有复杂的法条,只有极简的线条!一张画对应一个民法典词...