A swift impression on the Swift Tour (4) Objects, Classes, Inheritance

Xiaoli Shen
3 min readOct 22, 2017

To declare a class in Swift is easy. Just say class MyClass {} and type the properties and methods like simple variables and functions inside the curly braces.

To instantiate an object of a class is even easier. Unlike in many other languages you don’t even need the new keyword, just place a pair of parentheses after the class name and assign it to the constant or variable to hold the object. Then you can access its properties and methods with the good old dot notation.

It’s desirable to instantiate objects with different initial property values each to its own. This is where an init method, or a constructor in some other languages, comes into play. What’s more, you can also have a deinit method if you need to do some cleanup before trashing the object.

Now your objects are versatile. However, it doesn’t look too safe or professional to let the outside world simply access anything in them directly with the dot sign. Give them getters and setters so that they can compute their public properties and protect themselves.

Here’s an example of using getters and setters in a swift class. The setter gets the newValue automatically (you can also call it something else, e.g., you can call it newVal if you write the setter as set(newVal){…}). Then the setter can be used as a good place to do some validation job.

Remember, getters and setters can only be used for computed properties, which don’t actually get stored in the memory and therefore can not have default values.

The Swift language also provides two hook methods when setting a new value to a property: willSet and didSet. The former will be called just before and the latter immediately after setting the new value.

These two work both for stored properties and computed properties. The use case could be, for example, notifying other parts of the application about the value change as well as documenting the change in a log file.

Now all seems good and easy but … a bit useless, isn’t it? After all, what’s the point having classes if you don’t extend them with more functionalities? Yes, we need Inheritance, Protocols, Extensions!

Right, there are at least three ways to extend a class’s functionalities in Swift.

The Inheritance is the easiest to understand. Usually the parent and the child classes are modeling the same type of thing by design, with the parent being more abstract, i.e., the child being more specific. The shape family example illustrates this idea handily:

Both Triangle and Quadrilateral are a type of shape, so it’s natural to let them extend the same parent class Shape. They inherit all of their parent class’s properties and methods, and are free to add their own properties or override their parent’s method to their own content.

Pay attention to what’s happening in the child class’s init() method. To make sure nothing is accessed before initialized, things should be done in a certain order:

(1) assign the values to their own properties,
(2) call their parent’s init() method providing necessary values,
(3) assign values to properties they inherit from their parent (these are only accessible after the parent is initialized)

--

--

Xiaoli Shen

Solutions Architect at AWS with focus areas in MLOps and now-code-low-code ML. 10 yrs in tech and counting. Opinions and posts are my own.