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


相关内容

热门资讯

游戏驿站上调2026财年EBI... 游戏驿站更新业绩展望,市场讨论核心从“短期能否存续”转向“手握充足资本的公司能创造何种价值”,当前估...
Eshallgo(EHGO)大... 核心事件:Eshallgo(股票代码EHGO)盘中大幅暴跌,跌幅超21%,收盘报约3.05美元,本次...
特斯拉早盘涨超3% 深化与Sp...   特斯拉股价周一早盘上涨超3%,此前有消息称该公司正进一步深化与姊妹公司SpaceX的合作关系。截...
广钢气体:氦气小分子深地存储项... 广钢气体6月29日公告,公司股票自2026年6月9日至6月29日累计涨幅达74.51%,显著高于科创...
花旗维持对阿尔伯森的买入评级 ... 花旗集团分析师保罗·勒茹对艾伯森公司维持“买入”评级,将其目标价从22美元下调至17美元。译文内容由...