Function taking a variable number of arguments

swift

Solution

In case of all arguments are Int numbers: Int[] would be intuitive. But if you have code like this:

func foo(args:AnyObject...) {
    for arg: AnyObject in args {
        println(arg)
    }
}

foo(5, "bar", NSView())

output:

5
bar
<NSView: 0x7fc5c1f0b450>

Problem

In this document: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-XID_1 It mentions that when creating `for` loops we can use the shorthand of `0..3` and `0...3` to replace `i = 0; i < 3; ++i` and `i = 0; i <= 3; ++i` respectively. All very nice. Further down the document in the Functions and Closures section it says that functions can have a variable number of arguments passed via an array. However, in the code example we see the `...` again. ``` func sumOf(numbers: Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum } ``` Is this a mistake? It seems to me that a more intuitive syntax would be `numbers: Int[]`. A few examples down we see another code sample which has exactly that: ``` func hasAnyMatches(list: Int[], condition: Int -> Bool) -> Bool { ```

Original source

Related problems