passing unknown number of arguments in a function
Swift Variadic Parameters
accepts zero or more parameters of a specified type. Syntax of variadic parameters is, insert three period characters (...)
after the parameter’s type name.
func anyNumberOfTextField(_ textField: UITextField...) {
}
Now you can pass any number of textField.
anyNumberField(UITextField(),UITextField(), UITextField(), UITextField())
Note: A function may have at most one variadic parameter.
For more info check this Swift Functions
There is another way you can do that is called Array Parameter
. There are some pros and cons of these two methods. You will find the difference here link.
Passing an array to a function with variable number of args in Swift
Splatting is not in the language yet, as confirmed by the devs. Workaround for now is to use an overload or wait if you cannot add overloads.
Accessing number of parameters in a closure in Swift
Is it possible to get the number of closureArgs provided within test?
The Short Answer
No.
Slightly Longer Answer
No, it is not. Here's why:
The function is taking a closure as it's argument that by definition takes a variadic number of arguments. There's no possible way for someone calling the function to designate how many arguments the closure should take ahead of time. For example, if I were to call your function, it might look like this:
test() { (closureArgs: Double...) -> Double in
var n: Double = 0
for i in closureArgs {
n += i
}
return n
}
As you'll notice, I don't define the number of arguments anywhere because the number of arguments is only specified when the closure is called. Then the number can be determined inside, or possibly returned.
Basically, the closure is called within test, so only you the caller know how many arguments it takes. Anyone consuming this function has no control over it.
Swift arguments not allowed when function is assigned to a variable?
- Yes.
- See SE-0111 - Remove type system significance of function argument labels.
Example:
func foo(bar: Int, baz: String) {
print(bar, baz)
}
foo(bar: 123, baz: "abc") // Valid, prints: 123 abc
let x = foo //Inferred type: (Int, String) -> Void
x(123, "abc") // Valid, prints: 123 abc
let y: (bar: Int, baz: String) -> Void = foo // Invalid
// ERROR at line 10, col 9: function types cannot have argument label 'bar'; use '_' instead
// let y: (bar: Int, baz: String) -> Void
// ^
// _
// ERROR at line 10, col 19: function types cannot have argument label 'baz'; use '_' instead
// let y: (bar: Int, baz: String) -> Void
// ^
// _
y(bar: 123, baz: "abc") // Invalid
// ERROR at line 19, col 2: extraneous argument labels 'bar:baz:' in call
// y(bar: 123, baz: "abc") // 123 abc
// ^~~~~~ ~~~~~
Related Topics
How to Stop an Infinitely Rotating Image? and How Does One Implement Removeallanimations
How to Call Swiftui Navigationlink Conditionally
Moving an Object Across the Screen at a Certain Speed.(Sprite Kit)
How to Resume a Published Timer in Swiftui After Navigating to a Different Page
Cfdictionary Get Value for Key in Swift3
Is There a Neat Way to Represent a Fraction as an Attributed String
Testing If Double from Textfield Has a Value or Not
Problem Creating and Writing a Subdirectory in Swift 5
How to Generate a Random Unicode Character in Swift
Passing Unknown Number of Arguments in a Function
Image Disappears When Styling Class to Make a Round Image
How to Have Either or Wherefields for Firestore Query
iOS 13 Swiftui: App Crashes Upon Launch on Real Device