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


相关内容

热门资讯

俄副外长:俄美未就新版削减战略... △俄副外长里亚布科夫(资料图)当地时间2月9日,俄罗斯外交部副部长里亚布科夫表示,俄罗斯与美国之间并...
伊朗原子能组织主席:若所有制裁... (来源:上观新闻)伊朗当地媒体9日报道,伊朗原子能组织主席伊斯拉米当天表示,若能解除对伊朗的全部制裁...
47家非上市人身险公司去年实现...   本报记者 杨笑寒  近日,非上市人身险公司2025年四季度偿付能力报告陆续披露,相关企业经营业绩...
【新春走基层】年轻人玩出“年味... (来源:安徽新闻网)转自:安徽新闻网“二十三,糖瓜粘;二十四,扫房子……”当传统年俗遇上“Z世代”的...
“多彩黔峰·美在新时代”贵州美... 转自:贵州日报 本报讯(记者 陈江南)2月9日,“多彩黔峰·美在新时代”贵州美术大赛作品展在贵州美术...