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


相关内容

热门资讯

线上销量腰斩!苹果iPad猛猛... 过去提到平板电脑,很多人脑海里只有两个选项,要么iPad,要么“其他”。但自苹果今年6月底宣布对多款...
西藏吉隆3人遇难、265人失联... 转自:长安街知事8月26日10时30分许,因尼泊尔一侧发生泥石流灾害,造成西藏自治区日喀则市吉隆县吉...
重要通胀数据发布,美联储9月加...   8月26日晚间,美股三大指数集体下挫。  整体看,盘面并未出现热点板块,投资者在等待英伟达盘后发...
盛路通信:上半年净利润3644... 人民财讯8月26日电,盛路通信(002446)8月26日披露半年报,2026年上半年,公司实现营业收...
西藏启动自治区自然灾害一级救助... 00:18西藏自治区防灾减灾救灾委员会8月26日晚发布通知,26日10时30分许,因尼泊尔一侧发生泥...