I just wondered if there was some way to combine the different types and get this to work under one loop.
Absolutely. There's a standard technique for this.
What you're trying to do is loop through an array of views that have a settable textAlignment property. To make UILabel and UITextField be of that type, declare the type as a protocol with exactly that property, and conform UILabel and UITextField to it.
Now you can form UILabel and UITextField instances into an array of that same type, as is needed in Swift, and loop through it:
protocol Alignable : UIView {
var textAlignment : NSTextAlignment {get set}
}
extension UILabel : Alignable {}
extension UITextField : Alignable {}
let name = UILabel()
let age = UITextField()
for i in [name, age] as [Alignable] {
i.textAlignment = .center
}
The compiler permits this, because you've shown it, through the protocol, that what you're trying to do — namely, to assign to the textAlignment property of each of the things in the array — is possible. You're saying: "Hey, compiler, I know it looks like UITextField and UILabel don't have much in common, but think of them exactly as things with settable textAlignment properties, and no more."