Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I remove or disable some initialization from super class?

I want disable or remove some initialization from my super class, here my test codes:

class MyA {
    
    init() {
        
    }
    
    init(value: String) {
        
    }
    
    init(value2: String) {
        
    }
}


class MyB: MyA {
    
}

As you can see MyA has 3 initialization and MyB has the same because it is sub class, so I want remove 2 of this 3 initialization in MyB class, I want MyB just be initialize with init(), how can i do this? and the other 2 way be unavailable for sub class.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

As such, define a designated initializer.

class MyB: MyA {
  override init() {
    super.init()
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading