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


相关内容

热门资讯

容大感光:泰国基地规划进度、对... 投资者提问:东南亚 PCB 产能持续扩张,日系厂商在当地占据主导地位。公司泰国生产基地规划的油墨与干...
国家电网、南方电网达成我国首笔... (来源:电力国际汇epintl)按照国家发展改革委、国家能源局部署,6月18日,国家电网、南方电网达...
以军:打死扎扎和萨法迪!内塔尼... 据新华社,以色列国防军22日发表声明说,20日在加沙地带打死了两名巴勒斯坦伊斯兰抵抗运动(哈马斯)军...
浙江金华乡村“慢生活”吸引城市... 来源:中国新闻网 风铃祈福、夜间音乐会、农耕摸鱼体验......端午假期,在浙江金华经济技术开发区,...
画面公布:日本舰机滋扰挑衅,辽... 辽宁舰编队专业稳妥处置应对日本舰机滋扰挑衅6月22日,海军辽宁舰编队圆满完成远海实战化训练,安全顺利...