Set to tuples to avoid code repetition
suggest changeAvoid code repetition in constructors by setting a tuple of variables with a one liner. This sample is Swift 2. self cannot be used before super.init, so the static call is Contact.setUp(). setUp has to declare the tuple it returns. The labels and text view are not added to the view.
class Contact: UIView
{
private var message: UILabel
private var phone: UITextView
required init?(coder aDecoder: NSCoder) {
(message, phone) = Contact.setUp()
super.init(coder: aDecoder)
}
override func awakeFromNib() {
(message, phone) = Contact.setUp()
super.awakeFromNib()
}
override init(frame: CGRect) {
(message, phone) = Contact.setUp()
super.init(frame: frame)
}
private static func setUp() -> (UILabel, UITextView) {
let message = UILabel() // ...
let phone = UITextView() // ...
return (message, phone)
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents