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


相关内容

热门资讯

史无前例!美国出现人工智能失控... 7月25日,路透社援引知情人士消息刊发独家报道称,OpenAI一款AI Agent在入侵人工智能平台...
“六张网”如何赋能现代化产业体... 来源:人民网-人民日报最近,很多人在讨论“六张网”。“六张网”跟传统的基础设施建设有什么不同?为什么...
中国多地“伏羊节”集中开席 千... 中新社合肥7月26日电 (江婉琪)“我们喝了羊汤,计划再去皇藏峪转转,把‘伏羊节’的活动都体验一下。...
广东将防风Ⅰ级应急响应调整为防... 今年第12号台风“红霞”于7月26日3时50分前后以强台风级(45米/秒,14级)在惠州市惠东县平海...
海外之声 | 金融风险“非银化... 导读2008年全球金融危机后,对银行资本与流动性的监管强化的显著提升了银行体系韧性,但金融风险并未因...