【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月28日,美国白...
女孩近距离投喂狮子时受伤,汕头... (来源:上观新闻)近日,广东省汕头市中山公园内发生惊险一幕,一名女孩在近距离投喂狮子时,被狮子隔着金...
两家银行,同日官宣!行长兼任首... 每经记者|刘嘉魁    每经编辑|黄博文     2026年2月下旬,银行业合规治...
翼神龙卡垫获取方法 今天为大家分享实用技巧,希望能帮到各位,具体方法如下。1、 打开游戏王,进入主界面后点击竞技场选项开...
SQLite创建数据库表方法 在日常开发中,SQLite应用广泛,创建数据表是常见操作。只需使用CREATE TABLE语句,定义...