【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年,新疆城乡居民人均可支配收入分别增长5.3%、...
“万千星火护春运 法治同行守平... (来源:劳动午报)转自:劳动午报 昨天,由北京市天康戒毒康复所联合北京铁路公安局北京公安处北京西站派...
漂莱特创下衢州新速度 (来源:衢州日报)转自:衢州日报  本报讯 (记者 王继红 通讯员 叶超) 1月29日,位于智造新城...
伊朗首都启动战时避难设施建设规... 新华社德黑兰1月29日电(记者陈霄 沙达提)据伊朗媒体29日报道,伊朗首都德黑兰市政府已启动战时避难...