1

It may look a dumb question (sorry if it is), but usually, in my apps, I have this rootVC to inherit with others VCs. So came to me that I have multiples instances of this root. For example, in AppDelegate I call my first view as:

let window = UIWindow(frame: UIScreen.main.bounds)
let root = SplashViewController(nibName: "SplashViewController", bundle: nil)
window.rootViewController = UINavigationController(rootViewController: root)
window.makeKeyAndVisible()

self.window = window

Then SplashViewController inherits from my RootViewController where I can make some view configurations. But, when I call another VC (like InitialViewController) which also inherit from my root, I'm creating new instance from my root or using the same? And do you think that it is a good practice?

I was reading and searching but I couldn't find or understand clearly in the api reference: https://developer.apple.com/reference/uikit/uiviewcontroller

Any Suggestion? Thanks in advance!

werediver
  • 4,667
  • 1
  • 29
  • 49
Gehlen
  • 160
  • 1
  • 15

2 Answers2

1

Having a common subclass for all your view controllers could be useful, but try not to burden it too much. Also consider using composition instead of inheritance where possible and reasonable (Prefer composition over inheritance?).

Then SplashViewController inherits from my RootViewController where I can make some view configurations. But, when I call another VC (like InitialViewController) which also inherit from my root, I'm creating new instance from my root or using the same?

Don't worry. If you don't use static variables or variables the in global scope, then each instance of your view controllers will be independent.

Community
  • 1
  • 1
werediver
  • 4,667
  • 1
  • 29
  • 49
  • Thank you @werediver! Actually I think I've never heard about composition (or maybe had used but without know). So my mother class, which I will inherit, will be which kind? UIViewController? (couldn't, right?). To secure create that root (I've been using but never thought about this principals), I can't use static? Doesn't static allow me only one instantiation? My worry is instantiate the root every time besides calling the first one created - (generating memory leaks). If I'm not being clear, sorry, limited english here.. – Gehlen Jan 21 '17 at 16:07
  • @Gehlen composition, as well as inheritance, is an important concept of OOP, so try it :) Inheritance tree could be of arbitrary depth, like `UIViewController` -> `MyBaseViewController` -> `SettingsScreenViewController`. Each instance of `SettingsScreenViewController` will have all of `MyBaseViewController` and `MyBaseViewController`, but that's not a leak, it's by design. It allows each instance of `SettingsScreenViewController` to work independently. – werediver Jan 21 '17 at 16:16
  • Nice @werediver! My concept was wrong so.. I'll take a look at composition. Thanks again! – Gehlen Jan 21 '17 at 16:30
1

This is an example falling into the anti patterns of OOPS. In Swift you can address this with the use of protocols.

Say, you have a common class to inherit , what if some set of class needs a special functionality which others don't?. If you choose to add this in the common superclass you tend to bloat all other subclasses that inherit the super class. If you don't, you may need to duplicate the code.

You can approach the problem elegantly with the use of protocols, I think this how you would do,

protocol CommonTraits { }
extension CommonTraits { // provide default implementations of common methods }

protocol SpecialSetOfTraits { }
extension SpecialSetOfTraits {  // provide default implementations of common methods  }

Class A : CommonTraits {} // less bloating 
Class B : CommonTraits, SpecialSetOfTraits { } // Additional functionality. 

This is why i love Swift :)

Vignesh
  • 10,205
  • 2
  • 35
  • 73