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


相关内容

热门资讯

遇见丁宇翔·一个噪音案改判背后... 【遇见丁宇翔·一个噪音案改判背后:突破套路,守护实质公平】2025年6月,中央政法委印发通知,号召全...
节前蔬菜、猪肉价格回落,猪肉降... (来源:上观新闻)12月26日下午,记者走访上海最大肉类保供单位西郊国际农产品交易中心获悉,元旦佳节...
俄罗斯将车用汽油出口禁令再延长... △资料图  俄联邦政府新闻处12月27日通报,俄政府已于25日通过决议,将车用汽油临时出口禁令延长至...
“私募魔女”李蓓卖课12888... 12月26日,知名私募基金经理李蓓通过微信公众号“半夏投资”发布一则闭门分享会的活动介绍。报名页面显...
郝波:72个品牌跨界入局宠物行... 乐居财经 李兰 12月27日,“万物生、态不同”2025宠业新增长大会暨郝波宠业年度演讲在浙江安吉举...