設定為元組以避免程式碼重複
通過使用 one liner 設定變數元組,避免建構函式中的程式碼重複:
class Contact: UIView
{
private var message: UILabel
private var phone: UITextView
required init?(coder aDecoder: NSCoder) {
(message, phone) = self.dynamicType.setUp()
super.init(coder: aDecoder)
}
override func awakeFromNib() {
(message, phone) = self.dynamicType.setUp()
super.awakeFromNib()
}
override init(frame: CGRect) {
(message, phone) = self.dynamicType.setUp()
super.init(frame: frame)
}
private static func setUp(){
let message = UILabel() // ...
let phone = UITextView() // ...
return (message, phone)
}
}