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


相关内容

热门资讯

哪款空调划算 环保用户关注新一... 当夏日蝉鸣渐起,冬夜寒意悄然攀窗,节能环保关注者不再满足于基础冷暖——他们需要真正懂空气、惜能源、护...
2026空调性能排行榜 小空间... 夏日午后的宿舍楼道里,风扇嗡鸣、墙面微烫,而隔壁房间却静得只听见翻书声——那台悄然运行的空调正以近乎...
美股午盘走低道指下挫880点,...   北京时间7月30日凌晨,美股周三午盘继续下跌,道指跌逾880点。美国国债收益率走高。市场静待美联...
谎称认识领导能办理船牌号   本报讯 临高男子王某谎称“认识领导”,以帮助办理船牌号、申请造船指标为名,骗取表哥符某36.1万...
浙江坤博精工股东减持计划届满:... 2026年7月27日,浙江坤博精工科技股份有限公司(以下简称“公司”)发布股东减持股份结果公告,披露...