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


相关内容

热门资讯

华升泵阀取得轴流泵补偿系统相关... 6月6日消息,国家知识产权局信息显示,合肥华升泵阀股份有限公司申请一项名为“用于聚乙烯轴流泵的机封冲...
河南元丰科技申请精准灌溉系统相... 6月6日消息,国家知识产权局信息显示,河南元丰科技网络股份有限公司申请一项名为“基于机器学习的精准灌...
道尔智控取得平移闸相关专利,平... 6月6日消息,国家知识产权局信息显示,深圳市道尔智控科技股份有限公司申请一项名为“平移闸”的专利,授...
女承父业,珍宝岛的资金困局 (来源:投资者网-思维财经)【医药视界 】直击大健康行业动态与药企财报,一线政策解读,迅速看懂医药前...
凯东源物流申请电子签章法相关专... 6月6日消息,国家知识产权局信息显示,深圳市凯东源现代物流股份有限公司申请一项名为“电子签章方法、装...