如何在Swift中传递variadic参数

时间:2017-01-25 03:37:09

标签: swift parameters variadic

func sumOf(numbers: Int...) -> Int {
    var result = 0
    for number in numbers {
        result += number
    }
    return result
}

let a1 = sumOf(numbers: 1, 2, 4, 10)

func sampleOfSum(nums: Int...) -> Int {
    return sumOf(numbers: nums)// Error: Cannot convert value of type '[Int]' to expected argument type 'Int'
}

0 个答案:

没有答案