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


相关内容

热门资讯

空军招飞录取考生综合素养稳步提... (来源:千龙网)新华社北京7月30日电(高玉娇、蒋华峰)记者从空军招飞局获悉,2026年空军招飞录取...
荆门市专题研究“一张图”“一网... 转自:荆门发布7月30日,市委书记陈家伟在市城市运行管理中心主持召开会议,听取市县两级“一张图”建设...
天生桥(外一首) □邓太忠 盘古开天的时候,你一直在加油 声浪像蜒绵的山峦 荡过神州,也飘过大海 而你躬身的岁月,如一...
巫山龙门复线桥 明年上半年建成... 7月28日,重庆市级重点民生工程巫山龙门复线桥完成全部钢拱肋吊装,攻克项目核心施工难点,为后续合龙及...
《重庆市预防未成年人犯罪条例》... 《重庆市预防未成年人犯罪条例》(以下简称条例)于2026年7月30日经市六届人大常委会第二十五次会议...