【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月6日获融资买入73... 7月6日,能之光跌4.14%,成交额1326.48万元。两融数据显示,当日能之光获融资买入额73.9...
李某诉中远海能等案2026年7... 7月7日消息,天眼查数据显示,近日公布了一则中远海运能源运输股份有限公司作为被告/被上诉人的开庭公告...
中天火箭股东陕西电器1天减持1... 7月7日消息,中天火箭股东陕西电器于2026年7月3日,通过竞价交易方式减持15.61万股,占总股本...
虹口试点!小学专门局部改造教室... (来源:上观新闻)7月6日,上海市小学生爱心暑托班正式开班了,这项民心工程,切实解决不少小学生假期“...
圆通速递股东杭州灏月7天减持8... 7月7日消息,圆通速递股东杭州灏月于2026年6月30日至2026年7月6日,通过大宗交易方式减持8...