【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月4日—8日和1...
警方通报“少年在家和同学饮酒后... (来源:法治日报)7月3日晚,安徽淮南市公安局田家庵分局发布警情通报:关于网上报道“淮南一15岁少年...
中船先锋|七〇三所蒸汽动力事业...   在庆祝中国共产党成立105周年之际,党中央对全国“两优一先”进行表彰。中国船舶集团有限公司七〇三...
啤酒节里藏着“宝藏”文创市集   游客在选购文创产品。  本报讯(记者 李佳琪文/摄)7月3日,第二十四届哈尔滨国际啤酒节在哈尔滨...