Reputation: 425
I have a couple of textField outlet collections. I want to move through each collection and add a border to each textField. I can successfully do this one collection at a time. However, I'd like to not have to call the border function separately for each collection.
I have the following outletCollections
@IBOutlet var nameCollection: [UITextField]!
@IBOutlet var phoneCollection: [UITextField]!
This works for each collection
for name in nameCollection {
someFunction...
}
I'm trying to do something like this.
let collections = [nameCollection, phoneCollection]
for name in collections {
someFunction...
}
Basically I want to provide a list of collections and have the function performed on each member of each collection.
Upvotes: 0
Views: 432
Reputation: 89509
Just combine your outlet collections:
let combinedCollection = nameCollection + phoneCollection
For example:
extension UITextField
{
func doSomething()
{
}
}
class ViewController: UIViewController {
@IBOutlet var nameCollection: [UITextField]!
@IBOutlet var phoneCollection: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
let combinedCollection = nameCollection + phoneCollection
for eachField in combinedCollection
{
eachField.doSomething()
}
}
}
This example assumes that each collection has the same type of object.
Upvotes: 1