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


相关内容

热门资讯

一场精准的“政策捕捉” 传鼎晖... 观点网 上海苏州河畔,一幢服务式公寓悄然易主,掀开了住房租赁行业生存逻辑变革的一角。最新消息显示,1...
低利率时代理财变局:存款“搬家... 2025年5月,国有六大行定期存款一年期利率集体跌破1%关口;11月,五年期大额存单从多家银行产品列...
“万能”止痛药布洛芬,千万别乱... 转自:健康中国生活中,头疼、牙疼、关节痛等各种疼痛时常找上门,很多人会下意识地掏出布洛芬来缓解。虽然...
中芯国际:拟购买中芯北方49.... 中芯国际公告称,公司拟向国家集成电路基金等5名中芯北方股东发行股份购买其所持有的标的公司49.00%...
全球首艘万吨级近海新能源散货船... 滨州日报/滨州网讯 12月29日,全球首艘万吨级近海新能源散货船“魏桥绿动1”轮首航仪式在滨州市套尔...