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


相关内容

热门资讯

应对低利率挑战理财子公司“打新... (来源:经济参考报) 7月20日,长鑫科技中签投资者完成缴款。每个中签号码可认购500股,按8.66...
近3亿元股权转让终止 新华百货... (来源:经济参考报) 日前,银川新华百货商业集团股份有限公司(简称“新华百货”,600785.SH)...
LPR连续十四个月“按兵不动”... (来源:经济参考报) 7月20日,中国人民银行授权全国银行间同业拆借中心公布,2026年7月20日贷...
6月70城房价数据出炉 ...   三湘都市报7月20日讯 近日,国家统计局发布2026年6月份商品住宅销售价格变动情况,70个大中...
兴业500ETF季报解读:净值... 主要财务指标:本期利润近千万 资产净值3.99亿元报告期内(2026年4月1日-6月30日),兴业中...