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


相关内容

热门资讯

“外卖诗人”王计兵:“我能讲出... 中新网苏州7月6日电 (常慕城)“我希望我们能在日常生活中做一个发光的人,让自己明亮,也能照亮别人。...
霍尔木兹海峡有限通航但仍面临多... (来源:中国经济信息社)全球能源市场当前最关注的问题之一,当属霍尔木兹海峡通航究竟恢复到什么程度。多...
迪士尼怎么成了情侣分手第一现场 不知道你有没有遇到过:跟对象一起去迪士尼,原本计划要玩遍所有热门项目、拍满整个相册,结果从进园第一刻...
投资者提问:贵公司是否会履行法... 投资者提问:贵公司是否会履行法律责任,依法对1.31日持股股东索赔金额如数偿还?预计偿还金额多少?董...
到站了!天问二号抵达目标小行星... 来源:央视新闻客户端 一颗形似花生的陌生天体,正在黑暗中漂浮。一道来自中国的轨迹,精准地切入了它的轨...