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


相关内容

热门资讯

战事可能危及学校、电站、工业园... 4月4日,中国驻以色列大使馆发布消息:当前,中东安全形势高度紧张,战事扩大升级的风险仍然极高,并可能...
西安网评:31年徒步赴约不是单... 转自:西安网4月3日凌晨5点,天还未破晓,宁夏固原3000余名师生与社会人士列队肃立,在一声“出发”...
艾姓股民向喜临门发起索赔 刘彦...   受损股民可至Hehson股民维权平台登记该公司维权:http://wq.finance.sina...
最早投资张雪的人赚翻了 【#最早投资张雪的人赚翻了#】#张雪机车投资方已猛赚500%#一战封神,张雪机车点燃了国产摩托车市场...
有市民质疑“商家售卖‘人民币版... 清明假期,正是祭祀扫墓、出游踏青的好时节。有消费者称,在购买祭扫用品时竟发现了“人民币版”的冥币,这...