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


相关内容

热门资讯

山西稷山县西社镇马家沟村:盘活...   【为推进中国式现代化凝聚磅礴力量·走进乡村看振兴】  光明日报记者 张 楠 杨 珏 光明日报通讯...
作业本上印着师生笔下“最美校园...   光明日报记者 常 河 丁一鸣 光明日报通讯员 王亚玲  “这不是我的作品嘛!”刚开学领到作业本,...
甘肃:以正确政绩观引领高质量发...   光明日报记者 尚 杰 王冰雅  交房28年,却无法办理房产证。今年3月,甘肃白银市直面困难、合力...
三条高铁同日上新 中国交通基建... 9月1日,沪宁合高铁、津潍高铁、渝万高铁三条时速350公里的高铁线路在同一天迎来铺轨关键节点。从长三...
“只要肯钻研,普通工人也能拿大... (来源:工人日报) 近日,陕煤集团黄陵矿业公司第九届人才科技大会会场掌声连连,6家科技工作先进...