Andy
Andy

Reputation: 193

Swift class multiple inheritance

I created a class like this one:

class number1: UIScrollView {

init() {
    super.init(frame: CGRect(x: 9, y: 780, width: 1024, height: 267))
    self.contentSize = CGSize(width: 100, height: 267)
    self.backgroundColor = UIColor.clear
    self.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.RawValue(UInt8(UIView.AutoresizingMask.flexibleWidth.rawValue)))

    //followed by do blablabla I dont want in my new class

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Now I want a 2nd class similar to my 1st class but without the blablabla.

class number2: number1 {

override init() {
    super.init()
    self.frame = CGRect(x: 400, y: 10, width: 196, height: 500)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

I am doing it this way but dont know how to remove the "blablabla". The easiest would be to inherit the init from UIScrollView instead of class number1. How to do that?

Upvotes: 0

Views: 63

Answers (1)

JonJ
JonJ

Reputation: 1061

Just the bare bones here but should be enough:

class number1: UIScrollView {
    init() {
        super.init()
        // Do stuff common to all classes
        setup()
    }

    func setup() {
        // Do stuff for this class
    }
}


class number2: number1 {
    override func setup() {
        // Do stuff for this class only
    }
}

Upvotes: 4

Related Questions