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


相关内容

热门资讯

500亿粤港澳大湾区创投引导基... 21世纪经济报道记者 陈思琦 深圳报道12月26日,国家发展改革委、财政部发起设立的国家创业投资引导...
江西餐饮从业者话赣菜发展:融入... 转自:中国新闻网  江西餐饮从业者话赣菜发展:融入文化元素 开发新产品  中新网南昌12月26日电 ...
王忠林主持召开省委常委会会议 ... 12月26日下午,省委常委会召开会议,传达学习习近平总书记近期重要讲话、重要指示精神,研究部署我省贯...
外媒:马来西亚前总理被判罪名成... 参考消息网12月26日报道 据路透社12月26日报道,马来西亚前总理纳吉布26日被判所有滥用职权和洗...
跳马镇杨林新村:跨界同心 共绘...   长沙晚报掌上长沙12月26日讯(全媒体记者 彭放)翻开雨花区行政区划图,地处跳马镇最南端的杨林新...