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


相关内容

热门资讯

重大进展!伊朗官员确认:与阿曼... 转自:财联社财联社8月8日讯(编辑 刘蕊)本周五,一名美国官员声称,伊朗与阿曼之间谈判已经取得进展,...
一位意大利汉学家的非遗情缘(我... 来源:人民日报海外版唐云与温州市陶山镇民间瓯绣艺人合影。受访者供图唐云在读书。记者 刘军国摄唐云家中...
赵一德在调研乡村振兴工作时强调... 转自:陕西发布8月7日,省委书记赵一德到西安市、咸阳市调研乡村振兴工作。他强调,要深入学习贯彻习近平...
ST惠程秩鼎ESG评级较上年维... 8月8日消息,近期秩鼎发布最新ESG评级结果,ST惠程(维权)获得BBB级,相较于上一年的BBB级,...
ST八菱秩鼎ESG评级较上年下... 8月8日消息,近期秩鼎发布最新ESG评级结果,ST八菱获得BBB级,相较于上一年的A级,评级下降。从...