How to implement two inits with same content without code duplication in Swift?

后端 未结 7 1124
旧时难觅i
旧时难觅i 2020-12-01 00:10

Assume a class that is derived from UIView as follows:

class MyView: UIView {
    var myImageView: UIImageView

    init(frame: CGRect) {
               


        
相关标签:
7条回答
  • 2020-12-01 00:50

    How about this?

    public class MyView : UIView
    {
        var myImageView: UIImageView = UIImageView()
    
        private func setup()
        {
            myImageView.contentMode = UIViewContentMode.ScaleAspectFill
        }
    
        override public init(frame: CGRect)
        {
            super.init(frame: frame)
            setup()
        }
    
        required public init(coder aDecoder: NSCoder)
        {
            super.init(coder: aDecoder)
            setup()
        }
    }
    
    0 讨论(0)
提交回复
热议问题