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


相关内容

热门资讯

当APEC遇见深圳:《奇迹》如... (来源:直新闻)今年是亚太经合组织(APEC)“中国年”,也将是深圳首次举办APEC非正式领导人会议...
百果园集团(02411)稀释百... 百果园集团(02411)公布,于2026年2月2日,公司订立增资协议,据此田锡秋先生及赖显阳先生同意...
华纳兄弟股东据悉或在3月就奈飞...   北京时间2月2日晚间消息,据报道,华纳兄弟探索很可能在3月份就向Netflix出售其流媒体和制片...
周生生回应足金挂坠被检出含铁 【#周生生回应足金挂坠被检出含铁#】 #市监局介入周生生足金挂坠检出含铁# 近日,广东清远消费者李女...
中概指数跌幅扩大至1% 格隆汇2月2日|纳斯达克中国金龙指数跌幅扩大,现跌1.0%,最新报7652.47点。小鹏汽车跌超6%...