如何将Swift中的多参函数转化成Curring函数
来源:程序员人生 发布时间:2015-05-26 08:28:35 阅读次数:2406次
假定我们有1个含有多个参数的函数,用于构建1个人的基本信息:
func buildInfoWithName(name: String,#age: Int,#gender: String,#address: String,#phone: String) -> String {
return "My name is " + name
+ ",I'm a " + gender
+ ",I live in " + address
+ ",my phone number is " + phone
}
let mike = buildInfoWithName("Mike",age: 20,gender: "boy",address: "Tokyo Japan",phone: "12345678")
如果我们不想1次性提供所有的参数,可以把它改造成Curring函数:
func buildInfoWithName(name: String)(age: Int)(gender: String)(address: String)(phone: String) -> String {
return "My name is " + name
+ ",I'm a " + gender
+ ",I live in " + address
+ ",my phone number is " + phone
}
let benson = buildInfoWithName("Benson")(age: 24)(gender: "boy")(address: "Chengdu Sichuan")(phone: "87654321")
我们把原始的函数已分解成了接受单1参数的函数序列。
参考链接: http://justtesting.org/post/94325843216/what-is-currying-in-swift
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠