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


相关内容

热门资讯

【硅锰】矿价强势、钢厂追涨,供... (来源:合金现货网)本周硅锰市场大体呈强势上涨运行,从消息面情绪主导到供需基本面迎来实质性改善预期,...
伊朗总统:若基础设施或经济中心... 总台记者获悉,当地时间28日,伊朗总统佩泽希齐扬表示,如果伊朗的基础设施或经济中心成为攻击目标,伊方...
@辽宁驾驶人 6月1日起,这些... (来源:东北新闻网)近日公安部正式发布《机动车驾驶人疲劳驾驶认定规则》(GA/T 2372-2026...
“增长失速、收入迷雾、家族掌控... 一家宣称在高炉本体内衬细分市场占据半壁江山的公司,一份净利润两年高增长的成绩单,一个紧扣“碳中和”热...