Two ways to pass an array into a function:
1. Passing in a sequence
func sumOf(numbers: Int...) -> Int {
...
}
sumOf(42, 597, 12)
2. Passing in a Array object
func sumOf(numbers: [Int]) -> Int {
...
}
let numbers = [42, 597, 12]
sumOf(numbers)
In the 1st example, array is passed as a sequence of Int. In the 2nd example, an object of Array is required, otherwise, there will be an error. You may see that no matter you use Int... or [Int], numbers is an array inside the function.
No comments:
Post a Comment