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


相关内容

热门资讯

希拉里称一直呼吁公开爱泼斯坦文... 转自:京报网_北京日报官方网站 #希拉里回应爱泼斯坦案没...
奥巴马:“外星人确实存在,但…... 当地时间14日,在最新发布的访谈视频中,美国前总统奥巴马称“外星人确实存在”,但情况和外界的猜测有所...
视频丨两张列车时刻表跨越十年 ... (来源:千龙网) 今年春运,全国铁路平均每天上千万人次的...
我总领馆提醒:中国公民近期避免... 2月15日,大阪市中央区道顿堀发生一起持刀伤人事件,造成1人死亡、2人受伤,其中1人失去意识。15日...
火车票候补订单先到先得!没买到... (来源:半月谈)转自:半月谈 【#火车票候补订单先到先得...