Function taking a variable number of arguments

前端 未结 2 899
忘了有多久
忘了有多久 2021-02-07 02:32

In this document: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2

相关标签:
2条回答
  • 2021-02-07 02:49

    The type of parameter in sumOf are known as 'variadic' parameter. The parameters passed are accepted just as a group of elements and is then converted into array before using it within that function.

    A great example would be this post.

    Passing lists from one function to another in Swift

    0 讨论(0)
  • 2021-02-07 02:58

    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>
    
    0 讨论(0)
提交回复
热议问题