【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月18日电(记者尹亮)德国工商大会17日发布最新经济调查报告显示,受...
总台春晚硬核科技大盘点!这些技...   今年春晚以“骐骥驰骋 势不可挡”为主题,开启了一场跨越千年的文化之旅。在春晚舞台上,现代科技加持...
美媒:特朗普距离与伊朗爆发大规... 格隆汇2月18日|据AXIOS,特朗普政府距离在中东发动一场大规模战争比大多数美国人意识到的要近得多...
王珺任咸宁市生态环境局党组书记... (来源:中国环境网)转自:中国环境网据湖北省咸宁市生态环境局网站更新显示,王珺已任咸宁市生态环境局党...
千问“一句话下单”40%是00... 格隆汇2月18日|据硬AI,2月18日,千问APP公布,通过千问“一句话下单”的用户中,00后占比高...