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


相关内容

热门资讯

核电余热变工业蒸汽   本报讯(宜静 记者 张鸣霄)4月11日,哈电汽轮机自主研制的福清核电核能供汽项目核心设备——首批...
广州新一轮降雨时间确定,还要降... 据最新预测,这一周广州阳光少,体感闷热,雷雨出没。特别是4月15日以后,雨势渐趋明显,这个周末广州可...
霍尔木兹海峡,对峙升级 2026.04.13本文字数:1887,阅读时长大约3分钟美伊在伊斯兰堡的谈判于4月12日结束,双方...
德永佳集团获执行董事潘彬泽增持... .ct_hqimg {margin: 10px 0;} .hqimg_wrapper {text-a...
“群魔”混战,卫龙美味重回家族... 在辣条赛道当了多年带头大哥后,卫龙美味已悄然换挡——魔芋零食踩上减脂控卡的风口,迎来了爆发,取代辣条...