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


相关内容

热门资讯

春节给家里选电视还是换投影一文... 【ZOL原创技术】春节的脚步越来越近,家家户户都在忙着焕新家居、筹备团圆,而客厅作为全家欢聚的核心场...
台股开红盘在即,ETF规模翻倍... 经济观察网 截至2026年2月19日,台湾基金与股票市场近期值得关注的事件集中于台股长假后开红盘、E...
联合国强烈谴责以色列在约旦河西... 来源:央视新闻客户端当地时间2月18日,联合国巴勒斯坦人民行使不可剥夺权利委员会主席团发表声明,强烈...
2026年第一季度四大通信手机... 【ZOL中关村在线原创导购】进入2026年第一季度,手机行业的通信技术迎来了新一轮的升级突破,卫星通...
AMD Zen 6架构APU将... 【ZOL中关村在线原创新闻】近日,AMD 在提交给 LLVM 编译器的驱动代码中,披露了即将应用于Z...