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


相关内容

热门资讯

【专家视角】北京师范大学发文|...   Hehson财经ESG评级中心提供包括资讯、报告、培训、咨询等在内的14项ESG服务,助力上市公...
人民日报任平文章:为人民出政绩... (一)踏进新的一年、迈入“十五五”时期,面对深刻复杂变化的发展环境,我们要以什么样的奋斗,把今后5年...
地缘风险落地难破供过于求 委内...   分析师于周六表示,尽管美国总统特朗普通过军事行动推翻了石油资源丰富的委内瑞拉总统马杜罗的政权,但...
通航25年,“小三通”成为两岸... 【环球时报特约记者 陈立非】1月2日是两岸“小三通”客运航线通航满25周年,当日,厦门五通码头客运中...
法媒:这架飞机是中国南极科考的... 法国Media24网1月2日文章,原题:这不是普通飞机,而是中国十年来在南极运输的支柱  十年来,中...