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


相关内容

热门资讯

鼓励放宽儿童免票身高年龄限制 转自:成都日报锦观鼓励放宽儿童免票身高年龄限制两部门联合印发意见 推进儿童友好建设 17日,国...
北京28项举措打造伙伴式营商环... (来源:千龙网)昨天(17日),深化“北京服务”赋能企业高质量发展新闻发布会举行。记者从会上获悉,本...
警惕盲盒消费的概率陷阱   柳婉婷  中国消费者协会近日发布消费提示,直指盲盒消费领域的重重陷阱。数据显示,2025年“全国...
北京四环主路健康工程17日晚全... (来源:千龙网)记者昨(17)日从市交通委获悉,3月17日晚,北京市四环主路(健翔桥东-四元桥南、十...
感受国医魅力 (来源:中国妇女报)转自:中国妇女报  3月17日,在山东省青岛市市北区桦川路幼儿园开展的“神奇中草...