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


相关内容

热门资讯

世事繁华不及你惊鸿一瞥什么意思... 世事繁华不及你惊鸿一瞥什么意思?理解这句话首先要明白惊鸿一瞥的意思。那惊鸿一瞥什么意思呢——只是匆匆...
朋友梦见我在火里让她救我是什么... 朋友梦见我在火里让她救我是什么意思?如果你们是单纯的朋友可能说明友谊很好吧,如果不是单纯的,那可能是...
猜谜啦!猜谜啦!很多谜语哦! 猜谜啦!猜谜啦!很多谜语哦!1,开方2,3.4,5,圆周率6。王,8,丕9,10,丰11,晶12,偶...
女孩子说感觉有点怪 女孩子说感觉有点怪这对她来说比较突然,而且也许她现在自己也不能分辨出她对你的感觉到底是喜欢还是好朋友...
南京银行股份有限公司关于实施“... 证券简称: 南京银行 证券代码: 601009 编号: 2025-052优先股简称:南银优1 优先股...
科技赋能“气象+”,一场助企富... (来源:工人日报) “8月头季稻卖一块八毛,10月份再收一季,两季加起来一亩地收上千元!”四川...
取一杯天上水是指月光吗 取一杯天上水是指月光吗不是。是指下雨的时候接一杯天上掉下来的雨水,最纯洁的水最有灵性的水。这杯水在晚...
为什么越来越觉得赚钱比男朋友重... 为什么越来越觉得赚钱比男朋友重要?我也这么认为,因为男朋友不够好。还没遇到真心待自己的人或者,是因为...
腐团儿现实中有男朋友吗? 腐团儿现实中有男朋友吗?腐团儿现实中有男朋友吗?这个谁知道呢一或者暗中有也不一定这种明星私事我也不是...
元宵与汤圆有啥区别 元宵与汤圆有啥区别元宵和汤圆的区别:1.馅料选择不同,汤圆馅偏软,咸甜荤素选择众多,元宵馅偏硬,以甜...