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


相关内容

热门资讯

Meta正式入局个人AI代理赛... 财联社9月10日讯(编辑 赵昊)Meta Platforms推出了一款全新的人工智能代理,旨在帮助用...
兵团20名优秀教师获评特级教师 本报乌鲁木齐讯(全媒体记者 陈琼 常丫丫) 近日,兵团教育局、兵团人力资源和社会保障局联合发布...
打好政策“组合拳” 托起群众... 本报乌鲁木齐讯(全媒体记者 陈琼) “近期看房客户明显增多,叠加住房消费补贴政策带动,成交态势向...
2026显示器排行榜 高清IP... 深夜赶PPT、清晨抢网课、周末剪Vlog、睡前追剧刷番——对大学生而言,一块靠谱的显示器早已不是外设...
最新八G显卡销量排行榜 远程G... 清晨九点,城市写字楼里灯光渐次亮起,一台搭载专业显卡的台式机正悄然加载远程GPU服务——它不追求4K...