Passing an unknown number of arguments to functions
In my current project, I had to implement several functions to handle varying amounts UITextField
across multiple screens of my application.
I think it would be more efficient if I could implement one function that can take any number UITextField
.
Is it possible to implement this twilight function on the swift 3?
source to share
Swift Variadic Parameters
provides you with these capabilities. Write variable parameters by inserting three period characters (...)
after the parameter type name. Here I give you a demo:
func anyNumberOfTextField(_ textField: UITextField...) {
}
Call the function as follows. Now you can pass any number of text fields. I passed three parameters here:
anyNumberField(UITextField(),UITextField(), UITextField(), UITextField())
Note. A function can have at most one variation parameter.
See Swift Functions for details
source to share
I think you can do this by creating a function that takes a UITextField array as an argument.
func handle(textFields: [UITextField]) {
// do your things here by accessing each textFields like : textFields[0]
}
Otherwise, you will just create multiple arguments that are equal to the number of text boxes you have to do something to. Hope this helps :)
source to share