I have 10 textfields in my storyboard and I want to fetch values of them and store it into an array using loops so that I don’t want to write hard code for each textfield using swift 4 (Xcode)
. var arr = [Int]() let val1 = Int(TxtVal1.text ?? "0") ?? 0 let val2 = Int(TxtVal2.text ?? "0") ?? 0 let val3 = Int(TxtVal3.text ?? "0") ?? 0 let val4 = Int(TxtVal4.text ?? "0") ?? 0 let val5 = Int(TxtVal5.text ?? "0") ?? 0 arr = [val1,val2,val3,val4,val5]
don’t want to do it like that
2
Answers
You can assign tags to the UITextField and then store that textfield’s value to array.
Set the tags to textfields
Create and initialise array to store text of textfields
Use
@IBOutlet
collection. Bind your allUITextField
with the@IBOutlet
collection.Example: