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


相关内容

热门资讯

新西兰驻华大使:希望更多新西兰... “新西兰的年轻人很喜欢体验世界,我希望他们中有更多人能来中国看看。”新西兰驻华大使乔文博9日在北京如...
女子卖房子后一直未协助买方办理...   □记者 杨作品    本报讯 女子陈某将自己位于文昌市潭牛镇的房屋卖给符某后,一直未协助符某办理...
佛罗里达州在OpenAI IP...   佛罗里达州总检察长詹姆斯・乌特迈耶于周四对 OpenAI 及其聊天机器人 ChatGPT 启动调...
中国第42次南极考察实现多项突... (来源:千龙网)4月9日,由自然资源部组织的中国第42次南极考察队暨“雪龙”号极地考察破冰船顺利返回...
国风新材A股股东户数增加6.1... 4月10日消息,数据显示,截至2025年12月31日,国风新材A股股东总户数为11.75万户,较上期...