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


相关内容

热门资讯

十字对开门冰箱热卖排行榜 小户... 清晨阳光斜照进整洁的厨房,一碗隔夜炖好的银耳羹仍清润如初,青菜叶片挺括水灵,鸡蛋壳上还凝着细微水珠—...
最新冰箱性价比推荐 学生宿舍低... 清晨赶早课前顺手取一杯冰镇酸奶,深夜复习时打开冰箱拿一盒水果补充能量——对学生党而言,一台安静、省电...
再走长征路|心栖娄山:一个“候... 中经记者 王登海 黄永旭 遵义报道仲夏时节,黔北大地暑气渐盛,来自四川的退休教师赵俊毅提前20多天来...
铸魂聚力 实干兴业 (来源:沈阳日报)转自:沈阳日报  7月10日,全国五一劳动奖章获得者苏广林在中国工商银行沈阳南站支...
摩根大通AI投资组合回测跑赢传...   摩根大通最新发布的研究报告显示,其开发的八个人工智能投资代理在长达二十年的历史回测中,表现超越了...