Your Swift Classes Should Be Final

Make then final!

Steven Curtis
2 min readApr 26, 2022

--

Photo by George Kroeker on Unsplash

I’ve previously written about favouring composition over inheritance when coding in Swift.

I can even pull in my own definition of inheritance here:

Inheritance: A fundamental OO concept that enables new objects to take on the properties of existing objects. A class that inherits from a superclass is called a subclass or derived class.

So generally we don’t favour using inheritance when coding in Swift.

So what is the issue?

I remember that article! The basic point of that article was we should be implementing the SOLID principles while coding in Swift.

When we’re working in a team though, you can’t guarantee that your colleagues are going to follow all of that goodness.

When you create your rather fantastic class you will want to prevent other developers working on your code base (or yourself in the future) from overriding that class.

So my minimum UIViewController might look something like the following

and yes, it really is my habit to programmatically instantiate a UIViewController with a nib like that.

What if you use the Storyboard? Your most basic UIViewController might look more like the following:

--

--