【Swift 60秒】53 - Using closures as parameters when they accept parameters
创始人
2024-02-08 20:59:28
0

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


相关内容

热门资讯

“黑马”县的“金”点子 转自:中国银行保险报网□崔艳丽 本报记者 章丽铃长久以来,安徽省滁州市凤阳县因“朱元璋故里”“小岗村...
匠心守护绿茵烽火 智慧赋能城市...   初夏的江苏,绿茵场上烽火正燃。  由江苏省体育局与江苏省各设区市政府联合主办的江苏省城市足球联赛...
半月4家未盈利企业IPO获受理... 导读:虽然科创板、创业板已经开始受理未盈利企业IPO,但对于多数未盈利企业而言,港交所上市的可能性仍...
求一些恋爱养成游戏 求一些恋爱养成游戏翼之梦论坛知道吧?进去注册个号,有一个PC中文版的版块,进去什么都有。这个游戏不到...
为什么山上有老虎? 为什么山上有老虎?因为山上比较便于躲藏,也有各种小动物,是比较适合此类猛兽生活的环境
<<济南的冬天&g... <<济南的冬天>>板书设计有谁可以告诉我的板书设计在哪里可以找到?知到的朋友们请回答我好吗? 济...
带丈的四字成语 带丈的四字成语 火冒三丈软红十丈日落千磨颂迹丈尺水丈波百丈竿头食必方丈巾帼丈夫女中丈夫食前方丈白...
淮南:共同切磋比拼 技能大赛展... (转自:淮南日报)共同切磋比拼 技能大赛展风采“各位评委老师好,我们本次的参赛主题叫做《檐角飞云处 ...
幸福从天而降电视剧结局 幸福从天而降电视剧结局  1、《幸福从天而降》结局:天蓝用自己的经历告诫天晴,终于让天判孝晴打消了离...
51比50,副总统万斯投下关键... 转自:中安在线当地时间7月1日,美国国会参议院通过全面减税和支出法案,并提交众议院。美国参议院当天上...