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


相关内容

热门资讯

美俄同意恢复高级别军事对话 本文转自【新华网】;新华社阿布扎比2月5日电(记者赵丹亮 夏晓)美军欧洲司令部5日发表声明说,美国与...
固德电材系统(苏州)股份有限公... 有效报价是指网下投资者的报价不低于发行人和主承销商确定的发行价格,且未作为最高报价部分被剔除,同时符...
北京首创生态环保集团股份有限公... 证券代码:600008 证券简称:首创环保 公告编号:2026-005北京首创生态环保集团股份有限...
安徽新力金融股份有限公司关于为... 证券代码:600318 证券简称:新力金融 公告编号:临2026-003安徽新力金融股份有限公司关...
乌国防部长说 乌境内俄方“星链... (来源:衢州日报)转自:衢州日报  新华社基辅2月5日电 (记者 李东旭) 乌克兰国防部长费奥多罗夫...