0
点赞
收藏
分享

微信扫一扫

【Swift 60秒】43 - Variadic functions


0x00 Lesson

Some functions are ​​variadic​​​, which is a fancy way of saying they accept any number of parameters of the same type. The ​​print()​​​ function is actually ​​variadic​​: if you pass lots of parameters, they are all printed on one line with spaces between them:

print("Haters", "gonna", "hate")

You can make any parameter ​​variadic​​​ by writing ​​...​​​ after its type. So, an ​​Int​​​ parameter is a single integer, whereas ​​Int...​​ is zero or more integers - potentially hundreds.

Inside the function, Swift converts the values that were passed in to an ​​array​​ of integers, so you can loop over them as needed.

To try this out, let’s write a ​​square()​​ function that can square many numbers:

func square(numbers: Int...) {
for number in numbers {
print("\(number) squared is \(number * number)")
}
}

Now we can run that with lots of numbers just by passing them in separated by commas:

square(numbers: 1, 2, 3, 4, 5)

0x01 我的小作品

欢迎体验我的作品之一:​​小汉字-XHanzi​​​ 汉字书写入门,常用汉字 3800 个,二级字表 2200 个
​App Store​​ 搜索即可~


举报

相关推荐

0 条评论