【Swift 60秒】53 - Using closures as parameters when they accept parameters
创始人
2024-02-08 20:59:28
0

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


相关内容

热门资讯

宏创控股:截止2025年6月3... 投资者提问:你好,请问截止到2025年6月30日公司股东人数是多少?谢谢。董秘回答(宏创控股SZ00...
投资者提问:今年以来公司业务是... 投资者提问:今年以来公司业务是否平稳,未来胜帮是否继续给予大的支持?董秘回答(尤夫股份SZ00242...
盐湖股份:回应与汇信新材料项目... 投资者提问:董秘你好,汇信新材料的2万吨与公司的4万吨锂矿项目,双方公用的公共设施,配套设施,双方的...
红太阳:公司不生产高氯酸铵产品 投资者提问:董秘你好,公司生产高氯酸铵吗?产量大概多少董秘回答(红太阳SZ000525):尊敬的投资...
全球首台单机容量最大冲击式转轮... 中新网哈尔滨7月2日电 (王学善 魏子添)7月2日,全球首台单机容量最大500兆瓦、转轮尺寸最大6....
迈瑞医疗股东减持三轮套现50.... 长江商报消息 ●长江商报记者 沈右荣实习生 赖海军国内医疗器械巨头迈瑞医疗(300760.SZ)的股...
小商品城稳增长半年最高预盈17... 长江商报消息 ●长江商报记者 徐佳义乌市场景气度持续向好,小商品城(600415.SH)盈利能力的稳...
呈和科技终止重组股价三连阳 营... 长江商报消息 筹划的重大资产重组终止,多家A股公司复牌后股价大跌,呈和科技(688625.SH)收出...
投资者提问:请问,公司计划收购... 投资者提问:请问,公司计划收购的长空齿轮,其产品主要供应哪些行业?无人机,机器人,汽车,纺织机械,航...
云南大临铁路线路区间发生水害,... 7月2日,受强降雨影响,云南大(理)临(沧)铁路茂兰至云县区间发生水害,目前线路不具备通行条件,列车...