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


相关内容

热门资讯

比亚迪:公司海外业务保持良好增...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:证券时报)人...
特朗普要求伊朗举白旗投降 转自:财联社【特朗普要求伊朗举白旗投降】财联社8月17日电,据美国福克斯新闻今天(8月17日)报道,...
哈尔斯:上半年归母净利润488... 哈尔斯8月17日披露半年报,公司上半年实现营业收入17.73亿元,同比增长12.83%;归属于上市公...
飞力达拟4000万元至6000... 8月17日,飞力达公告,公司拟以集中竞价交易方式回购股份。回购金额不低于4000万元且不超过6000...
远东传动半年报解读:扣非净利润... 财报解读营业收入:商用车市场复苏驱动增长25.94%2026年上半年,远东传动实现营业收入9.05亿...