In my current project, I had to implement multiple functions to handle the different number of UITextField
in multiple screens of my application.
在我当前的项目中,我必须实现多个函数来在我的应用程序的多个屏幕中处理不同数量的UITextField。
I guess it would be more efficient if I can implement one function that can take any number of UITextField
.
我想如果我可以实现一个可以占用任意数量的UITextField的函数会更有效率。
Is it possible to implement this surd of functions on swift 3?
是否有可能在swift 3上实现这些功能?
3 个解决方案
#1
10
Swift Variadic Parameters
gives you that facilities. Write variadic parameters by inserting three period characters (...)
after the parameter’s type name. Here I give you a demo:
Swift Variadic Parameters为您提供了这些设施。通过在参数的类型名称后插入三个句点字符(...)来写入可变参数。在这里,我给你一个演示:
func anyNumberOfTextField(_ textField: UITextField...) {
}
Call the function like this. Now you can pass any number of textField. Here I have passed three parameters:
像这样调用这个函数。现在你可以传递任意数量的textField。这里我传递了三个参数:
anyNumberField(UITextField(),UITextField(), UITextField(), UITextField())
Note: A function may have at most one variadic parameter.
注意:函数最多可以包含一个可变参数。
For more info check this Swift Functions
有关更多信息,请查看此Swift函数
#2
5
The easiest would be passing an array of UITextFields:
最简单的方法是传递一组UITextField:
func processTextFields(fields:[UITextField]) {
for field in fields {
// Process your text fields
}
}
#3
0
I guess you can do so by creating a function that takes an array of UITextField as argument.
我想你可以通过创建一个以UITextField数组作为参数的函数来实现。
func handle(textFields: [UITextField]) {
// do your things here by accessing each textFields like : textFields[0]
}
Otherwise you would just create a number of arguments that equal your number of text fields you have to do things on. Hope this helps :)
否则,您只需创建一些参数,这些参数等于您必须执行操作的文本字段数。希望这可以帮助 :)
#1
10
Swift Variadic Parameters
gives you that facilities. Write variadic parameters by inserting three period characters (...)
after the parameter’s type name. Here I give you a demo:
Swift Variadic Parameters为您提供了这些设施。通过在参数的类型名称后插入三个句点字符(...)来写入可变参数。在这里,我给你一个演示:
func anyNumberOfTextField(_ textField: UITextField...) {
}
Call the function like this. Now you can pass any number of textField. Here I have passed three parameters:
像这样调用这个函数。现在你可以传递任意数量的textField。这里我传递了三个参数:
anyNumberField(UITextField(),UITextField(), UITextField(), UITextField())
Note: A function may have at most one variadic parameter.
注意:函数最多可以包含一个可变参数。
For more info check this Swift Functions
有关更多信息,请查看此Swift函数
#2
5
The easiest would be passing an array of UITextFields:
最简单的方法是传递一组UITextField:
func processTextFields(fields:[UITextField]) {
for field in fields {
// Process your text fields
}
}
#3
0
I guess you can do so by creating a function that takes an array of UITextField as argument.
我想你可以通过创建一个以UITextField数组作为参数的函数来实现。
func handle(textFields: [UITextField]) {
// do your things here by accessing each textFields like : textFields[0]
}
Otherwise you would just create a number of arguments that equal your number of text fields you have to do things on. Hope this helps :)
否则,您只需创建一些参数,这些参数等于您必须执行操作的文本字段数。希望这可以帮助 :)