【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月13日获悉,工业和信息化部、中央网信办等四部门近日联合印发《关于推动互...
美国债市:短端领跌国债 美联储...   美国国债走低,延续早盘跌势,此前美联储理事克里斯托弗·沃勒表示,如果基础通胀继续表明价格压力广泛...
从“低估”到“重估” 创新药迎... (来源:经济参考报) 7月13日,在大盘整体承压下行的背景下,创新药板块逆势走强。万邦医药、陇神戎发...
斯泰兰蒂斯(上海)汽车有限公司... 7月13日,斯泰兰蒂斯(上海)汽车有限公司正式获得商务部核发的货物自动进口许可,本次行政办理流程已全...
SpaceX股价连续第二日下跌...   核心要点SpaceX 股价连续第二个交易日走低,这家埃隆・马斯克旗下企业股价距离 135 美元 ...