Variable Number of Parameters in Swift Method -
i want make method can take variable number of parameters. kind of in javascript can parameters using arguments
keyword inside function. how can in swift?
my main purpose allow overloaded constructors go 1 method if has no parameters passed in @ all.
class counter { var count: int = 0 func incrementby(amount: int, numberoftimes: int) { count += amount * numberoftimes } }
there example of in swift ibook, page 17:
func sumof(numbers: int...) -> int { var sum = 0 number in numbers { sum += number } return sum } sumof() // returns 0 sumof(1,2) // returns 3 sumof(42, 597, 12) // returns 651
Comments
Post a Comment