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


相关内容

热门资讯

从试点迈向规模化落地,Clou... 2023年,人工智能迎来技术大爆炸;2024年,行业步入应用落地的探索阶段;2025年,全产业躬身入...
俊裕地基(01757)计划于中... 俊裕地基(01757)发布公告,因应中国民众消费水平提升及人口老化趋势,本集团将积极配合国家政策,计...
中国经济信心说丨奔向中国式现代... 转自:央视新闻客户端一年之计在于春。立春前一天,中央一号文件如期发布。这是党的十八大以来,连续14年...
巴基斯坦首都爆炸事件已致31死... 据央视新闻消息,当地时间6日,一名巴基斯坦官员称,当天早些时候该国首都伊斯兰堡一座清真寺发生的爆炸事...
顺络电子(002138.SZ)... 格隆汇2月6日丨顺络电子(002138.SZ)公布,公司作为有限合伙人与普通合伙人国投创新投资管理有...