0

During iOS development I'm using a lot of classes which come from proprietary code which I have no access to. I want to run custom code that should run when this class is instantiated into an object, but I don't know how to, as I can't really override their init without knowing what parameters said init takes.

For example I am working on a SceneDelegate which subclasses UIResponder and UIWindowSceneDelegate and I want to have a property which should get initialized when the class is first instantiated, but I don't know how to.

At the moment I am just defaulting to defining the property as optional, initialize it in the method that will first use it, and write a whole bunch of code all around this class to handle the fact that it is unnecessarily optional.

Is there a better way?

Thanks!

Hamster
  • 133
  • 3
  • 12

1 Answers1

1

You may try to make a class extension with your own init()?

extension SceneDelegate {
   init(someParam: ...) {
      ....
   }
}
ossamacpp
  • 656
  • 5
  • 11
  • But I would still have to have the normal init run, so the SceneDelegate continues to work nicely with iOS right? I don't know how to keep the normal init running as usual, and just run my custom init after it. – Hamster Apr 18 '20 at 20:25