Set to tuples to avoid code repetition

suggest change

Avoid 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)
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents